Mercurial > emacs
annotate src/cm.c @ 32856:5fe3b3dc5dd9
(overlays_at): Only let CHANGE_REQ inhibit an assignment of startpos to
prev when startpos == pos.
author | Miles Bader <miles@gnu.org> |
---|---|
date | Wed, 25 Oct 2000 07:30:21 +0000 |
parents | 10dd42348df3 |
children | a94852aef620 |
rev | line source |
---|---|
484 | 1 /* Cursor motion subroutines for GNU Emacs. |
11235 | 2 Copyright (C) 1985, 1995 Free Software Foundation, Inc. |
484 | 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 | |
14186
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
11235
diff
changeset
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
11235
diff
changeset
|
20 Boston, MA 02111-1307, USA. */ |
484 | 21 |
22 | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
484
diff
changeset
|
23 #include <config.h> |
484 | 24 #include <stdio.h> |
25 #include "cm.h" | |
26 #include "termhooks.h" | |
27 | |
25734
0242362bb1fe
(toplevel) [HAVE_TERMCAP_H]: Include termcap.h.
Gerd Moellmann <gerd@gnu.org>
parents:
25333
diff
changeset
|
28 #ifdef HAVE_TERMCAP_H |
0242362bb1fe
(toplevel) [HAVE_TERMCAP_H]: Include termcap.h.
Gerd Moellmann <gerd@gnu.org>
parents:
25333
diff
changeset
|
29 #include <termcap.h> |
30915
10dd42348df3
[HAVE_TERMCAP_H]: Include <termcap.h>.
Dave Love <fx@gnu.org>
parents:
29909
diff
changeset
|
30 #else |
10dd42348df3
[HAVE_TERMCAP_H]: Include <termcap.h>.
Dave Love <fx@gnu.org>
parents:
29909
diff
changeset
|
31 extern void tputs P_ ((const char *, int, int (*)(int))); |
10dd42348df3
[HAVE_TERMCAP_H]: Include <termcap.h>.
Dave Love <fx@gnu.org>
parents:
29909
diff
changeset
|
32 extern char *tgoto P_ ((const char *, int, int)); |
25734
0242362bb1fe
(toplevel) [HAVE_TERMCAP_H]: Include termcap.h.
Gerd Moellmann <gerd@gnu.org>
parents:
25333
diff
changeset
|
33 #endif |
0242362bb1fe
(toplevel) [HAVE_TERMCAP_H]: Include termcap.h.
Gerd Moellmann <gerd@gnu.org>
parents:
25333
diff
changeset
|
34 |
484 | 35 #define BIG 9999 /* 9999 good on VAXen. For 16 bit machines |
36 use about 2000.... */ | |
37 | |
38 extern char *BC, *UP; | |
39 | |
40 int cost; /* sums up costs */ | |
41 | |
42 /* ARGSUSED */ | |
21514 | 43 int |
484 | 44 evalcost (c) |
45 char c; | |
46 { | |
47 cost++; | |
8985
2825665b8352
(evalcost, cmputc): They now return their arguments.
Richard M. Stallman <rms@gnu.org>
parents:
7107
diff
changeset
|
48 return c; |
484 | 49 } |
50 | |
21514 | 51 int |
484 | 52 cmputc (c) |
53 char c; | |
54 { | |
55 if (termscript) | |
56 fputc (c & 0177, termscript); | |
57 putchar (c & 0177); | |
8985
2825665b8352
(evalcost, cmputc): They now return their arguments.
Richard M. Stallman <rms@gnu.org>
parents:
7107
diff
changeset
|
58 return c; |
484 | 59 } |
60 | |
61 /* NEXT TWO ARE DONE WITH MACROS */ | |
62 #if 0 | |
63 /* | |
64 * Assume the cursor is at row row, column col. Normally used only after | |
65 * clearing the screen, when the cursor is at (0, 0), but what the heck, | |
66 * let's let the guy put it anywhere. | |
67 */ | |
68 | |
69 static | |
70 at (row, col) { | |
71 curY = row; | |
72 curX = col; | |
73 } | |
74 | |
75 /* | |
76 * Add n columns to the current cursor position. | |
77 */ | |
78 | |
79 static | |
80 addcol (n) { | |
81 curX += n; | |
82 | |
83 /* | |
84 * If cursor hit edge of screen, what happened? | |
85 * N.B.: DO NOT!! write past edge of screen. If you do, you | |
86 * deserve what you get. Furthermore, on terminals with | |
87 * autowrap (but not magicwrap), don't write in the last column | |
88 * of the last line. | |
89 */ | |
90 | |
91 if (curX == Wcm.cm_cols) { | |
92 /* | |
93 * Well, if magicwrap, still there, past the edge of the | |
94 * screen (!). If autowrap, on the col 0 of the next line. | |
95 * Otherwise on last column. | |
96 */ | |
97 | |
98 if (Wcm.cm_magicwrap) | |
99 ; /* "limbo" */ | |
100 else if (Wcm.cm_autowrap) { | |
101 curX = 0; | |
102 curY++; /* Beware end of screen! */ | |
103 } | |
104 else | |
105 curX--; | |
106 } | |
107 } | |
108 #endif | |
109 | |
110 /* | |
10437 | 111 * Terminals with magicwrap (xn) don't all behave identically. |
112 * The VT100 leaves the cursor in the last column but will wrap before | |
113 * printing the next character. I hear that the Concept terminal does | |
114 * the wrap immediately but ignores the next newline it sees. And some | |
115 * terminals just have buggy firmware, and think that the cursor is still | |
116 * in limbo if we use direct cursor addressing from the phantom column. | |
117 * The only guaranteed safe thing to do is to emit a CRLF immediately | |
118 * after we reach the last column; this takes us to a known state. | |
119 */ | |
120 void | |
121 cmcheckmagic () | |
122 { | |
123 if (curX == FrameCols) | |
124 { | |
125 if (!MagicWrap || curY >= FrameRows - 1) | |
126 abort (); | |
127 if (termscript) | |
128 putc ('\r', termscript); | |
129 putchar ('\r'); | |
130 if (termscript) | |
131 putc ('\n', termscript); | |
132 putchar ('\n'); | |
133 curX = 0; | |
134 curY++; | |
135 } | |
136 } | |
137 | |
138 | |
139 /* | |
484 | 140 * (Re)Initialize the cost factors, given the output speed of the terminal |
141 * in the variable ospeed. (Note: this holds B300, B9600, etc -- ie stuff | |
142 * out of <sgtty.h>.) | |
143 */ | |
144 | |
21514 | 145 void |
484 | 146 cmcostinit () |
147 { | |
148 char *p; | |
149 | |
150 #define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG) | |
151 #define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e))) | |
152 | |
153 Wcm.cc_up = COST (Wcm.cm_up, evalcost); | |
154 Wcm.cc_down = COST (Wcm.cm_down, evalcost); | |
155 Wcm.cc_left = COST (Wcm.cm_left, evalcost); | |
156 Wcm.cc_right = COST (Wcm.cm_right, evalcost); | |
157 Wcm.cc_home = COST (Wcm.cm_home, evalcost); | |
158 Wcm.cc_cr = COST (Wcm.cm_cr, evalcost); | |
159 Wcm.cc_ll = COST (Wcm.cm_ll, evalcost); | |
160 Wcm.cc_tab = Wcm.cm_tabwidth ? COST (Wcm.cm_tab, evalcost) : BIG; | |
161 | |
162 /* | |
163 * These last three are actually minimum costs. When (if) they are | |
164 * candidates for the least-cost motion, the real cost is computed. | |
165 * (Note that "0" is the assumed to generate the minimum cost. | |
166 * While this is not necessarily true, I have yet to see a terminal | |
167 * for which is not; all the terminals that have variable-cost | |
168 * cursor motion seem to take straight numeric values. --ACT) | |
169 */ | |
170 | |
171 Wcm.cc_abs = CMCOST (Wcm.cm_abs, evalcost); | |
172 Wcm.cc_habs = CMCOST (Wcm.cm_habs, evalcost); | |
173 Wcm.cc_vabs = CMCOST (Wcm.cm_vabs, evalcost); | |
174 | |
175 #undef CMCOST | |
176 #undef COST | |
177 } | |
178 | |
179 /* | |
180 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using | |
181 * up and down, and left and right, motions, and tabs. If doit is set | |
182 * actually perform the motion. | |
183 */ | |
184 | |
21514 | 185 static int |
484 | 186 calccost (srcy, srcx, dsty, dstx, doit) |
187 { | |
188 register int deltay, | |
189 deltax, | |
190 c, | |
191 totalcost; | |
192 int ntabs, | |
193 n2tabs, | |
194 tabx, | |
195 tab2x, | |
196 tabcost; | |
197 register char *p; | |
198 | |
199 /* If have just wrapped on a terminal with xn, | |
200 don't believe the cursor position: give up here | |
201 and force use of absolute positioning. */ | |
202 | |
203 if (curX == Wcm.cm_cols) | |
204 goto fail; | |
205 | |
206 totalcost = 0; | |
207 if ((deltay = dsty - srcy) == 0) | |
208 goto x; | |
209 if (deltay < 0) | |
210 p = Wcm.cm_up, c = Wcm.cc_up, deltay = -deltay; | |
211 else | |
212 p = Wcm.cm_down, c = Wcm.cc_down; | |
213 if (c == BIG) { /* caint get thar from here */ | |
214 if (doit) | |
215 printf ("OOPS"); | |
216 return c; | |
217 } | |
218 totalcost = c * deltay; | |
219 if (doit) | |
220 while (--deltay >= 0) | |
221 tputs (p, 1, cmputc); | |
222 x: | |
223 if ((deltax = dstx - srcx) == 0) | |
224 goto done; | |
225 if (deltax < 0) { | |
226 p = Wcm.cm_left, c = Wcm.cc_left, deltax = -deltax; | |
227 goto dodelta; /* skip all the tab junk */ | |
228 } | |
229 /* Tabs (the toughie) */ | |
230 if (Wcm.cc_tab >= BIG || !Wcm.cm_usetabs) | |
231 goto olddelta; /* forget it! */ | |
232 | |
233 /* | |
234 * ntabs is # tabs towards but not past dstx; n2tabs is one more | |
235 * (ie past dstx), but this is only valid if that is not past the | |
236 * right edge of the screen. We can check that at the same time | |
237 * as we figure out where we would be if we use the tabs (which | |
238 * we will put into tabx (for ntabs) and tab2x (for n2tabs)). | |
239 */ | |
240 | |
241 ntabs = (deltax + srcx % Wcm.cm_tabwidth) / Wcm.cm_tabwidth; | |
242 n2tabs = ntabs + 1; | |
243 tabx = (srcx / Wcm.cm_tabwidth + ntabs) * Wcm.cm_tabwidth; | |
244 tab2x = tabx + Wcm.cm_tabwidth; | |
245 | |
246 if (tab2x >= Wcm.cm_cols) /* too far (past edge) */ | |
247 n2tabs = 0; | |
248 | |
249 /* | |
250 * Now set tabcost to the cost for using ntabs, and c to the cost | |
251 * for using n2tabs, then pick the minimum. | |
252 */ | |
253 | |
254 /* cost for ntabs + cost for right motion */ | |
255 tabcost = ntabs ? ntabs * Wcm.cc_tab + (dstx - tabx) * Wcm.cc_right | |
256 : BIG; | |
257 | |
258 /* cost for n2tabs + cost for left motion */ | |
259 c = n2tabs ? n2tabs * Wcm.cc_tab + (tab2x - dstx) * Wcm.cc_left | |
260 : BIG; | |
261 | |
262 if (c < tabcost) /* then cheaper to overshoot & back up */ | |
263 ntabs = n2tabs, tabcost = c, tabx = tab2x; | |
264 | |
265 if (tabcost >= BIG) /* caint use tabs */ | |
266 goto newdelta; | |
267 | |
268 /* | |
269 * See if tabcost is less than just moving right | |
270 */ | |
271 | |
272 if (tabcost < (deltax * Wcm.cc_right)) { | |
273 totalcost += tabcost; /* use the tabs */ | |
274 if (doit) | |
275 while (--ntabs >= 0) | |
276 tputs (Wcm.cm_tab, 1, cmputc); | |
277 srcx = tabx; | |
278 } | |
279 | |
280 /* | |
281 * Now might as well just recompute the delta. | |
282 */ | |
283 | |
284 newdelta: | |
285 if ((deltax = dstx - srcx) == 0) | |
286 goto done; | |
287 olddelta: | |
288 if (deltax > 0) | |
289 p = Wcm.cm_right, c = Wcm.cc_right; | |
290 else | |
291 p = Wcm.cm_left, c = Wcm.cc_left, deltax = -deltax; | |
292 | |
293 dodelta: | |
294 if (c == BIG) { /* caint get thar from here */ | |
295 fail: | |
296 if (doit) | |
297 printf ("OOPS"); | |
298 return BIG; | |
299 } | |
300 totalcost += c * deltax; | |
301 if (doit) | |
302 while (--deltax >= 0) | |
303 tputs (p, 1, cmputc); | |
304 done: | |
305 return totalcost; | |
306 } | |
307 | |
308 #if 0 | |
309 losecursor () | |
310 { | |
311 curY = -1; | |
312 } | |
313 #endif | |
314 | |
315 #define USEREL 0 | |
316 #define USEHOME 1 | |
317 #define USELL 2 | |
318 #define USECR 3 | |
319 | |
21514 | 320 void |
484 | 321 cmgoto (row, col) |
322 { | |
323 int homecost, | |
324 crcost, | |
325 llcost, | |
326 relcost, | |
327 directcost; | |
328 int use; | |
329 char *p, | |
330 *dcm; | |
331 | |
332 /* First the degenerate case */ | |
333 if (row == curY && col == curX) /* already there */ | |
334 return; | |
335 | |
336 if (curY >= 0 && curX >= 0) | |
337 { | |
338 /* We may have quick ways to go to the upper-left, bottom-left, | |
339 * start-of-line, or start-of-next-line. Or it might be best to | |
340 * start where we are. Examine the options, and pick the cheapest. | |
341 */ | |
342 | |
343 relcost = calccost (curY, curX, row, col, 0); | |
344 use = USEREL; | |
345 if ((homecost = Wcm.cc_home) < BIG) | |
346 homecost += calccost (0, 0, row, col, 0); | |
347 if (homecost < relcost) | |
348 relcost = homecost, use = USEHOME; | |
349 if ((llcost = Wcm.cc_ll) < BIG) | |
350 llcost += calccost (Wcm.cm_rows - 1, 0, row, col, 0); | |
351 if (llcost < relcost) | |
352 relcost = llcost, use = USELL; | |
353 if ((crcost = Wcm.cc_cr) < BIG) { | |
354 if (Wcm.cm_autolf) | |
355 if (curY + 1 >= Wcm.cm_rows) | |
356 crcost = BIG; | |
357 else | |
358 crcost += calccost (curY + 1, 0, row, col, 0); | |
359 else | |
360 crcost += calccost (curY, 0, row, col, 0); | |
361 } | |
362 if (crcost < relcost) | |
363 relcost = crcost, use = USECR; | |
364 directcost = Wcm.cc_abs, dcm = Wcm.cm_abs; | |
365 if (row == curY && Wcm.cc_habs < BIG) | |
366 directcost = Wcm.cc_habs, dcm = Wcm.cm_habs; | |
367 else if (col == curX && Wcm.cc_vabs < BIG) | |
368 directcost = Wcm.cc_vabs, dcm = Wcm.cm_vabs; | |
369 } | |
370 else | |
371 { | |
372 directcost = 0, relcost = 100000; | |
373 dcm = Wcm.cm_abs; | |
374 } | |
375 | |
376 /* | |
377 * In the following comparison, the = in <= is because when the costs | |
378 * are the same, it looks nicer (I think) to move directly there. | |
379 */ | |
380 if (directcost <= relcost) | |
381 { | |
382 /* compute REAL direct cost */ | |
383 cost = 0; | |
384 p = dcm == Wcm.cm_habs ? tgoto (dcm, row, col) : | |
385 tgoto (dcm, col, row); | |
386 tputs (p, 1, evalcost); | |
387 if (cost <= relcost) | |
388 { /* really is cheaper */ | |
389 tputs (p, 1, cmputc); | |
390 curY = row, curX = col; | |
391 return; | |
392 } | |
393 } | |
394 | |
395 switch (use) | |
396 { | |
397 case USEHOME: | |
398 tputs (Wcm.cm_home, 1, cmputc); | |
399 curY = 0, curX = 0; | |
400 break; | |
401 | |
402 case USELL: | |
403 tputs (Wcm.cm_ll, 1, cmputc); | |
404 curY = Wcm.cm_rows - 1, curX = 0; | |
405 break; | |
406 | |
407 case USECR: | |
408 tputs (Wcm.cm_cr, 1, cmputc); | |
409 if (Wcm.cm_autolf) | |
410 curY++; | |
411 curX = 0; | |
412 break; | |
413 } | |
414 | |
415 (void) calccost (curY, curX, row, col, 1); | |
416 curY = row, curX = col; | |
417 } | |
418 | |
419 /* Clear out all terminal info. | |
420 Used before copying into it the info on the actual terminal. | |
421 */ | |
422 | |
21514 | 423 void |
484 | 424 Wcm_clear () |
425 { | |
426 bzero (&Wcm, sizeof Wcm); | |
427 UP = 0; | |
428 BC = 0; | |
429 } | |
430 | |
431 /* | |
432 * Initialized stuff | |
433 * Return 0 if can do CM. | |
434 * Return -1 if cannot. | |
435 * Return -2 if size not specified. | |
436 */ | |
437 | |
21514 | 438 int |
484 | 439 Wcm_init () |
440 { | |
441 #if 0 | |
442 if (Wcm.cm_abs && !Wcm.cm_ds) | |
443 return 0; | |
444 #endif | |
445 if (Wcm.cm_abs) | |
446 return 0; | |
447 /* Require up and left, and, if no absolute, down and right */ | |
448 if (!Wcm.cm_up || !Wcm.cm_left) | |
449 return - 1; | |
450 if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right)) | |
451 return - 1; | |
452 /* Check that we know the size of the screen.... */ | |
453 if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0) | |
454 return - 2; | |
455 return 0; | |
456 } |