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