Mercurial > emacs
annotate src/cm.c @ 10040:c69ddd9fa3a6
(Man-bgproc-sentinel): No need for save-match-data.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 22 Nov 1994 04:37:43 +0000 |
parents | 2825665b8352 |
children | 3021721d1d69 |
rev | line source |
---|---|
484 | 1 /* Cursor motion subroutines for GNU Emacs. |
2 Copyright (C) 1985 Free Software Foundation, Inc. | |
3 based primarily on public domain code written by Chris Torek | |
4 | |
5 This file is part of GNU Emacs. | |
6 | |
7 GNU Emacs is free software; you can redistribute it and/or modify | |
8 it under the terms of the GNU General Public License as published by | |
7107 | 9 the Free Software Foundation; either version 2, or (at your option) |
484 | 10 any later version. |
11 | |
12 GNU Emacs is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with GNU Emacs; see the file COPYING. If not, write to | |
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | |
21 | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
484
diff
changeset
|
22 #include <config.h> |
484 | 23 #include <stdio.h> |
24 #include "cm.h" | |
25 #include "termhooks.h" | |
26 | |
27 #define BIG 9999 /* 9999 good on VAXen. For 16 bit machines | |
28 use about 2000.... */ | |
29 | |
30 char *tgoto (); | |
31 | |
32 extern char *BC, *UP; | |
33 | |
34 int cost; /* sums up costs */ | |
35 | |
36 /* ARGSUSED */ | |
37 evalcost (c) | |
38 char c; | |
39 { | |
40 cost++; | |
8985
2825665b8352
(evalcost, cmputc): They now return their arguments.
Richard M. Stallman <rms@gnu.org>
parents:
7107
diff
changeset
|
41 return c; |
484 | 42 } |
43 | |
44 cmputc (c) | |
45 char c; | |
46 { | |
47 if (termscript) | |
48 fputc (c & 0177, termscript); | |
49 putchar (c & 0177); | |
8985
2825665b8352
(evalcost, cmputc): They now return their arguments.
Richard M. Stallman <rms@gnu.org>
parents:
7107
diff
changeset
|
50 return c; |
484 | 51 } |
52 | |
53 /* NEXT TWO ARE DONE WITH MACROS */ | |
54 #if 0 | |
55 /* | |
56 * Assume the cursor is at row row, column col. Normally used only after | |
57 * clearing the screen, when the cursor is at (0, 0), but what the heck, | |
58 * let's let the guy put it anywhere. | |
59 */ | |
60 | |
61 static | |
62 at (row, col) { | |
63 curY = row; | |
64 curX = col; | |
65 } | |
66 | |
67 /* | |
68 * Add n columns to the current cursor position. | |
69 */ | |
70 | |
71 static | |
72 addcol (n) { | |
73 curX += n; | |
74 | |
75 /* | |
76 * If cursor hit edge of screen, what happened? | |
77 * N.B.: DO NOT!! write past edge of screen. If you do, you | |
78 * deserve what you get. Furthermore, on terminals with | |
79 * autowrap (but not magicwrap), don't write in the last column | |
80 * of the last line. | |
81 */ | |
82 | |
83 if (curX == Wcm.cm_cols) { | |
84 /* | |
85 * Well, if magicwrap, still there, past the edge of the | |
86 * screen (!). If autowrap, on the col 0 of the next line. | |
87 * Otherwise on last column. | |
88 */ | |
89 | |
90 if (Wcm.cm_magicwrap) | |
91 ; /* "limbo" */ | |
92 else if (Wcm.cm_autowrap) { | |
93 curX = 0; | |
94 curY++; /* Beware end of screen! */ | |
95 } | |
96 else | |
97 curX--; | |
98 } | |
99 } | |
100 #endif | |
101 | |
102 /* | |
103 * (Re)Initialize the cost factors, given the output speed of the terminal | |
104 * in the variable ospeed. (Note: this holds B300, B9600, etc -- ie stuff | |
105 * out of <sgtty.h>.) | |
106 */ | |
107 | |
108 cmcostinit () | |
109 { | |
110 char *p; | |
111 | |
112 #define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG) | |
113 #define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e))) | |
114 | |
115 Wcm.cc_up = COST (Wcm.cm_up, evalcost); | |
116 Wcm.cc_down = COST (Wcm.cm_down, evalcost); | |
117 Wcm.cc_left = COST (Wcm.cm_left, evalcost); | |
118 Wcm.cc_right = COST (Wcm.cm_right, evalcost); | |
119 Wcm.cc_home = COST (Wcm.cm_home, evalcost); | |
120 Wcm.cc_cr = COST (Wcm.cm_cr, evalcost); | |
121 Wcm.cc_ll = COST (Wcm.cm_ll, evalcost); | |
122 Wcm.cc_tab = Wcm.cm_tabwidth ? COST (Wcm.cm_tab, evalcost) : BIG; | |
123 | |
124 /* | |
125 * These last three are actually minimum costs. When (if) they are | |
126 * candidates for the least-cost motion, the real cost is computed. | |
127 * (Note that "0" is the assumed to generate the minimum cost. | |
128 * While this is not necessarily true, I have yet to see a terminal | |
129 * for which is not; all the terminals that have variable-cost | |
130 * cursor motion seem to take straight numeric values. --ACT) | |
131 */ | |
132 | |
133 Wcm.cc_abs = CMCOST (Wcm.cm_abs, evalcost); | |
134 Wcm.cc_habs = CMCOST (Wcm.cm_habs, evalcost); | |
135 Wcm.cc_vabs = CMCOST (Wcm.cm_vabs, evalcost); | |
136 | |
137 #undef CMCOST | |
138 #undef COST | |
139 } | |
140 | |
141 /* | |
142 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using | |
143 * up and down, and left and right, motions, and tabs. If doit is set | |
144 * actually perform the motion. | |
145 */ | |
146 | |
147 static | |
148 calccost (srcy, srcx, dsty, dstx, doit) | |
149 { | |
150 register int deltay, | |
151 deltax, | |
152 c, | |
153 totalcost; | |
154 int ntabs, | |
155 n2tabs, | |
156 tabx, | |
157 tab2x, | |
158 tabcost; | |
159 register char *p; | |
160 | |
161 /* If have just wrapped on a terminal with xn, | |
162 don't believe the cursor position: give up here | |
163 and force use of absolute positioning. */ | |
164 | |
165 if (curX == Wcm.cm_cols) | |
166 goto fail; | |
167 | |
168 totalcost = 0; | |
169 if ((deltay = dsty - srcy) == 0) | |
170 goto x; | |
171 if (deltay < 0) | |
172 p = Wcm.cm_up, c = Wcm.cc_up, deltay = -deltay; | |
173 else | |
174 p = Wcm.cm_down, c = Wcm.cc_down; | |
175 if (c == BIG) { /* caint get thar from here */ | |
176 if (doit) | |
177 printf ("OOPS"); | |
178 return c; | |
179 } | |
180 totalcost = c * deltay; | |
181 if (doit) | |
182 while (--deltay >= 0) | |
183 tputs (p, 1, cmputc); | |
184 x: | |
185 if ((deltax = dstx - srcx) == 0) | |
186 goto done; | |
187 if (deltax < 0) { | |
188 p = Wcm.cm_left, c = Wcm.cc_left, deltax = -deltax; | |
189 goto dodelta; /* skip all the tab junk */ | |
190 } | |
191 /* Tabs (the toughie) */ | |
192 if (Wcm.cc_tab >= BIG || !Wcm.cm_usetabs) | |
193 goto olddelta; /* forget it! */ | |
194 | |
195 /* | |
196 * ntabs is # tabs towards but not past dstx; n2tabs is one more | |
197 * (ie past dstx), but this is only valid if that is not past the | |
198 * right edge of the screen. We can check that at the same time | |
199 * as we figure out where we would be if we use the tabs (which | |
200 * we will put into tabx (for ntabs) and tab2x (for n2tabs)). | |
201 */ | |
202 | |
203 ntabs = (deltax + srcx % Wcm.cm_tabwidth) / Wcm.cm_tabwidth; | |
204 n2tabs = ntabs + 1; | |
205 tabx = (srcx / Wcm.cm_tabwidth + ntabs) * Wcm.cm_tabwidth; | |
206 tab2x = tabx + Wcm.cm_tabwidth; | |
207 | |
208 if (tab2x >= Wcm.cm_cols) /* too far (past edge) */ | |
209 n2tabs = 0; | |
210 | |
211 /* | |
212 * Now set tabcost to the cost for using ntabs, and c to the cost | |
213 * for using n2tabs, then pick the minimum. | |
214 */ | |
215 | |
216 /* cost for ntabs + cost for right motion */ | |
217 tabcost = ntabs ? ntabs * Wcm.cc_tab + (dstx - tabx) * Wcm.cc_right | |
218 : BIG; | |
219 | |
220 /* cost for n2tabs + cost for left motion */ | |
221 c = n2tabs ? n2tabs * Wcm.cc_tab + (tab2x - dstx) * Wcm.cc_left | |
222 : BIG; | |
223 | |
224 if (c < tabcost) /* then cheaper to overshoot & back up */ | |
225 ntabs = n2tabs, tabcost = c, tabx = tab2x; | |
226 | |
227 if (tabcost >= BIG) /* caint use tabs */ | |
228 goto newdelta; | |
229 | |
230 /* | |
231 * See if tabcost is less than just moving right | |
232 */ | |
233 | |
234 if (tabcost < (deltax * Wcm.cc_right)) { | |
235 totalcost += tabcost; /* use the tabs */ | |
236 if (doit) | |
237 while (--ntabs >= 0) | |
238 tputs (Wcm.cm_tab, 1, cmputc); | |
239 srcx = tabx; | |
240 } | |
241 | |
242 /* | |
243 * Now might as well just recompute the delta. | |
244 */ | |
245 | |
246 newdelta: | |
247 if ((deltax = dstx - srcx) == 0) | |
248 goto done; | |
249 olddelta: | |
250 if (deltax > 0) | |
251 p = Wcm.cm_right, c = Wcm.cc_right; | |
252 else | |
253 p = Wcm.cm_left, c = Wcm.cc_left, deltax = -deltax; | |
254 | |
255 dodelta: | |
256 if (c == BIG) { /* caint get thar from here */ | |
257 fail: | |
258 if (doit) | |
259 printf ("OOPS"); | |
260 return BIG; | |
261 } | |
262 totalcost += c * deltax; | |
263 if (doit) | |
264 while (--deltax >= 0) | |
265 tputs (p, 1, cmputc); | |
266 done: | |
267 return totalcost; | |
268 } | |
269 | |
270 #if 0 | |
271 losecursor () | |
272 { | |
273 curY = -1; | |
274 } | |
275 #endif | |
276 | |
277 #define USEREL 0 | |
278 #define USEHOME 1 | |
279 #define USELL 2 | |
280 #define USECR 3 | |
281 | |
282 cmgoto (row, col) | |
283 { | |
284 int homecost, | |
285 crcost, | |
286 llcost, | |
287 relcost, | |
288 directcost; | |
289 int use; | |
290 char *p, | |
291 *dcm; | |
292 | |
293 /* First the degenerate case */ | |
294 if (row == curY && col == curX) /* already there */ | |
295 return; | |
296 | |
297 if (curY >= 0 && curX >= 0) | |
298 { | |
299 /* We may have quick ways to go to the upper-left, bottom-left, | |
300 * start-of-line, or start-of-next-line. Or it might be best to | |
301 * start where we are. Examine the options, and pick the cheapest. | |
302 */ | |
303 | |
304 relcost = calccost (curY, curX, row, col, 0); | |
305 use = USEREL; | |
306 if ((homecost = Wcm.cc_home) < BIG) | |
307 homecost += calccost (0, 0, row, col, 0); | |
308 if (homecost < relcost) | |
309 relcost = homecost, use = USEHOME; | |
310 if ((llcost = Wcm.cc_ll) < BIG) | |
311 llcost += calccost (Wcm.cm_rows - 1, 0, row, col, 0); | |
312 if (llcost < relcost) | |
313 relcost = llcost, use = USELL; | |
314 if ((crcost = Wcm.cc_cr) < BIG) { | |
315 if (Wcm.cm_autolf) | |
316 if (curY + 1 >= Wcm.cm_rows) | |
317 crcost = BIG; | |
318 else | |
319 crcost += calccost (curY + 1, 0, row, col, 0); | |
320 else | |
321 crcost += calccost (curY, 0, row, col, 0); | |
322 } | |
323 if (crcost < relcost) | |
324 relcost = crcost, use = USECR; | |
325 directcost = Wcm.cc_abs, dcm = Wcm.cm_abs; | |
326 if (row == curY && Wcm.cc_habs < BIG) | |
327 directcost = Wcm.cc_habs, dcm = Wcm.cm_habs; | |
328 else if (col == curX && Wcm.cc_vabs < BIG) | |
329 directcost = Wcm.cc_vabs, dcm = Wcm.cm_vabs; | |
330 } | |
331 else | |
332 { | |
333 directcost = 0, relcost = 100000; | |
334 dcm = Wcm.cm_abs; | |
335 } | |
336 | |
337 /* | |
338 * In the following comparison, the = in <= is because when the costs | |
339 * are the same, it looks nicer (I think) to move directly there. | |
340 */ | |
341 if (directcost <= relcost) | |
342 { | |
343 /* compute REAL direct cost */ | |
344 cost = 0; | |
345 p = dcm == Wcm.cm_habs ? tgoto (dcm, row, col) : | |
346 tgoto (dcm, col, row); | |
347 tputs (p, 1, evalcost); | |
348 if (cost <= relcost) | |
349 { /* really is cheaper */ | |
350 tputs (p, 1, cmputc); | |
351 curY = row, curX = col; | |
352 return; | |
353 } | |
354 } | |
355 | |
356 switch (use) | |
357 { | |
358 case USEHOME: | |
359 tputs (Wcm.cm_home, 1, cmputc); | |
360 curY = 0, curX = 0; | |
361 break; | |
362 | |
363 case USELL: | |
364 tputs (Wcm.cm_ll, 1, cmputc); | |
365 curY = Wcm.cm_rows - 1, curX = 0; | |
366 break; | |
367 | |
368 case USECR: | |
369 tputs (Wcm.cm_cr, 1, cmputc); | |
370 if (Wcm.cm_autolf) | |
371 curY++; | |
372 curX = 0; | |
373 break; | |
374 } | |
375 | |
376 (void) calccost (curY, curX, row, col, 1); | |
377 curY = row, curX = col; | |
378 } | |
379 | |
380 /* Clear out all terminal info. | |
381 Used before copying into it the info on the actual terminal. | |
382 */ | |
383 | |
384 Wcm_clear () | |
385 { | |
386 bzero (&Wcm, sizeof Wcm); | |
387 UP = 0; | |
388 BC = 0; | |
389 } | |
390 | |
391 /* | |
392 * Initialized stuff | |
393 * Return 0 if can do CM. | |
394 * Return -1 if cannot. | |
395 * Return -2 if size not specified. | |
396 */ | |
397 | |
398 Wcm_init () | |
399 { | |
400 #if 0 | |
401 if (Wcm.cm_abs && !Wcm.cm_ds) | |
402 return 0; | |
403 #endif | |
404 if (Wcm.cm_abs) | |
405 return 0; | |
406 /* Require up and left, and, if no absolute, down and right */ | |
407 if (!Wcm.cm_up || !Wcm.cm_left) | |
408 return - 1; | |
409 if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right)) | |
410 return - 1; | |
411 /* Check that we know the size of the screen.... */ | |
412 if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0) | |
413 return - 2; | |
414 return 0; | |
415 } |