This source file includes following definitions.
- evalcost
- cmputc
- at
- addcol
- cmcheckmagic
- cmcostinit
- calccost
- losecursor
- cmgoto
- Wcm_clear
- Wcm_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include <config.h>
22
23 #include "lisp.h"
24 #include "cm.h"
25 #include "sysstdio.h"
26 #include "termchar.h"
27 #include "tparam.h"
28
29 #define BIG 9999
30
31 int cost;
32
33 int
34 evalcost (int c)
35 {
36 cost++;
37 return c;
38 }
39
40
41 struct tty_display_info *current_tty;
42
43 int
44 cmputc (int c)
45 {
46 if (current_tty->termscript)
47 putc (c & 0177, current_tty->termscript);
48 putc (c & 0177, current_tty->output);
49 return c;
50 }
51
52
53 #if 0
54
55
56
57
58
59
60 static
61 at (tty, row, col) {
62 curY (tty) = row;
63 curX (tty) = col;
64 }
65
66
67
68
69
70 static
71 addcol (tty, n) {
72 curX (tty) += n;
73
74
75
76
77
78
79
80
81
82 if (curX (tty) == tty->Wcm->cm_cols) {
83
84
85
86
87
88
89 if (tty->Wcm->cm_magicwrap)
90 ;
91 else if (tty->Wcm->cm_autowrap) {
92 curX (tty) = 0;
93 curY (tty) ++;
94 }
95 else
96 curX (tty)--;
97 }
98 }
99 #endif
100
101
102
103
104
105
106
107
108
109
110
111 void
112 cmcheckmagic (struct tty_display_info *tty)
113 {
114 if (curX (tty) == FrameCols (tty))
115 {
116 if (!MagicWrap (tty) || curY (tty) >= FrameRows (tty) - 1)
117 emacs_abort ();
118 if (tty->termscript)
119 putc ('\r', tty->termscript);
120 putc ('\r', tty->output);
121 if (tty->termscript)
122 putc ('\n', tty->termscript);
123 putc ('\n', tty->output);
124 curX (tty) = 0;
125 curY (tty)++;
126 }
127 }
128
129
130
131
132
133
134
135
136 void
137 cmcostinit (struct tty_display_info *tty)
138 {
139 char *p;
140
141 #define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG)
142 #define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e)))
143
144 tty->Wcm->cc_up = COST (tty->Wcm->cm_up, evalcost);
145 tty->Wcm->cc_down = COST (tty->Wcm->cm_down, evalcost);
146 tty->Wcm->cc_left = COST (tty->Wcm->cm_left, evalcost);
147 tty->Wcm->cc_right = COST (tty->Wcm->cm_right, evalcost);
148 tty->Wcm->cc_home = COST (tty->Wcm->cm_home, evalcost);
149 tty->Wcm->cc_cr = COST (tty->Wcm->cm_cr, evalcost);
150 tty->Wcm->cc_ll = COST (tty->Wcm->cm_ll, evalcost);
151 tty->Wcm->cc_tab = tty->Wcm->cm_tabwidth ? COST (tty->Wcm->cm_tab, evalcost) : BIG;
152
153
154
155
156
157
158
159
160
161
162 tty->Wcm->cc_abs = CMCOST (tty->Wcm->cm_abs, evalcost);
163 tty->Wcm->cc_habs = CMCOST (tty->Wcm->cm_habs, evalcost);
164 tty->Wcm->cc_vabs = CMCOST (tty->Wcm->cm_vabs, evalcost);
165
166 #undef CMCOST
167 #undef COST
168 }
169
170
171
172
173
174
175
176 static int
177 calccost (struct tty_display_info *tty,
178 int srcy, int srcx, int dsty, int dstx, int doit)
179 {
180 register int deltay,
181 deltax,
182 c,
183 totalcost;
184 int ntabs,
185 n2tabs,
186 tabx,
187 tab2x,
188 tabcost;
189 register const char *p;
190
191
192
193
194
195 if (curX (tty) == tty->Wcm->cm_cols)
196 goto fail;
197
198 totalcost = 0;
199 if ((deltay = dsty - srcy) == 0)
200 goto x;
201 if (deltay < 0)
202 p = tty->Wcm->cm_up, c = tty->Wcm->cc_up, deltay = -deltay;
203 else
204 p = tty->Wcm->cm_down, c = tty->Wcm->cc_down;
205 if (c == BIG) {
206 if (doit)
207 fputs ("OOPS", stdout);
208 return c;
209 }
210 totalcost = c * deltay;
211 if (doit)
212 do
213 emacs_tputs (tty, p, 1, cmputc);
214 while (--deltay > 0);
215 x:
216 if ((deltax = dstx - srcx) == 0)
217 goto done;
218 if (deltax < 0) {
219 p = tty->Wcm->cm_left, c = tty->Wcm->cc_left, deltax = -deltax;
220 goto dodelta;
221 }
222
223 if (tty->Wcm->cc_tab >= BIG || !tty->Wcm->cm_usetabs)
224 goto olddelta;
225
226
227
228
229
230
231
232
233
234 ntabs = (deltax + srcx % tty->Wcm->cm_tabwidth) / tty->Wcm->cm_tabwidth;
235 n2tabs = ntabs + 1;
236 tabx = (srcx / tty->Wcm->cm_tabwidth + ntabs) * tty->Wcm->cm_tabwidth;
237 tab2x = tabx + tty->Wcm->cm_tabwidth;
238
239 if (tab2x >= tty->Wcm->cm_cols)
240 n2tabs = 0;
241
242
243
244
245
246
247
248 tabcost = ntabs ? ntabs * tty->Wcm->cc_tab + (dstx - tabx) * tty->Wcm->cc_right
249 : BIG;
250
251
252 c = n2tabs ? n2tabs * tty->Wcm->cc_tab + (tab2x - dstx) * tty->Wcm->cc_left
253 : BIG;
254
255 if (c < tabcost)
256 ntabs = n2tabs, tabcost = c, tabx = tab2x;
257
258 if (tabcost >= BIG)
259 goto newdelta;
260
261
262
263
264
265 if (tabcost < (deltax * tty->Wcm->cc_right)) {
266 totalcost += tabcost;
267 if (doit)
268 while (--ntabs >= 0)
269 emacs_tputs (tty, tty->Wcm->cm_tab, 1, cmputc);
270 srcx = tabx;
271 }
272
273
274
275
276
277 newdelta:
278 if ((deltax = dstx - srcx) == 0)
279 goto done;
280 olddelta:
281 if (deltax > 0)
282 p = tty->Wcm->cm_right, c = tty->Wcm->cc_right;
283 else
284 p = tty->Wcm->cm_left, c = tty->Wcm->cc_left, deltax = -deltax;
285
286 dodelta:
287 if (c == BIG) {
288 fail:
289 if (doit)
290 fputs ("OOPS", stdout);
291 return BIG;
292 }
293 totalcost += c * deltax;
294 if (doit)
295 do
296 emacs_tputs (tty, p, 1, cmputc);
297 while (--deltax > 0);
298 done:
299 return totalcost;
300 }
301
302 #if 0
303 void
304 losecursor (void)
305 {
306 curY = -1;
307 }
308 #endif
309
310 #define USEREL 0
311 #define USEHOME 1
312 #define USELL 2
313 #define USECR 3
314
315 void
316 cmgoto (struct tty_display_info *tty, int row, int col)
317 {
318 int homecost,
319 crcost,
320 llcost,
321 relcost,
322 directcost;
323 int use UNINIT;
324 char *p;
325 const char *dcm;
326
327
328 if (row == curY (tty) && col == curX (tty))
329 return;
330
331 if (curY (tty) >= 0 && curX (tty) >= 0)
332 {
333
334
335
336
337
338 relcost = calccost (tty, curY (tty), curX (tty), row, col, 0);
339 use = USEREL;
340 if ((homecost = tty->Wcm->cc_home) < BIG)
341 homecost += calccost (tty, 0, 0, row, col, 0);
342 if (homecost < relcost)
343 relcost = homecost, use = USEHOME;
344 if ((llcost = tty->Wcm->cc_ll) < BIG)
345 llcost += calccost (tty, tty->Wcm->cm_rows - 1, 0, row, col, 0);
346 if (llcost < relcost)
347 relcost = llcost, use = USELL;
348 if ((crcost = tty->Wcm->cc_cr) < BIG) {
349 if (tty->Wcm->cm_autolf)
350 if (curY (tty) + 1 >= tty->Wcm->cm_rows)
351 crcost = BIG;
352 else
353 crcost += calccost (tty, curY (tty) + 1, 0, row, col, 0);
354 else
355 crcost += calccost (tty, curY (tty), 0, row, col, 0);
356 }
357 if (crcost < relcost)
358 relcost = crcost, use = USECR;
359 directcost = tty->Wcm->cc_abs, dcm = tty->Wcm->cm_abs;
360 if (row == curY (tty) && tty->Wcm->cc_habs < BIG)
361 directcost = tty->Wcm->cc_habs, dcm = tty->Wcm->cm_habs;
362 else if (col == curX (tty) && tty->Wcm->cc_vabs < BIG)
363 directcost = tty->Wcm->cc_vabs, dcm = tty->Wcm->cm_vabs;
364 }
365 else
366 {
367 directcost = 0, relcost = 100000;
368 dcm = tty->Wcm->cm_abs;
369 }
370
371
372
373
374
375 if (directcost <= relcost)
376 {
377
378 cost = 0;
379 p = (dcm == tty->Wcm->cm_habs
380 ? tgoto (dcm, row, col)
381 : tgoto (dcm, col, row));
382 emacs_tputs (tty, p, 1, evalcost);
383 if (cost <= relcost)
384 {
385 emacs_tputs (tty, p, 1, cmputc);
386 curY (tty) = row, curX (tty) = col;
387 return;
388 }
389 }
390
391 switch (use)
392 {
393 case USEHOME:
394 emacs_tputs (tty, tty->Wcm->cm_home, 1, cmputc);
395 curY (tty) = 0, curX (tty) = 0;
396 break;
397
398 case USELL:
399 emacs_tputs (tty, tty->Wcm->cm_ll, 1, cmputc);
400 curY (tty) = tty->Wcm->cm_rows - 1, curX (tty) = 0;
401 break;
402
403 case USECR:
404 emacs_tputs (tty, tty->Wcm->cm_cr, 1, cmputc);
405 if (tty->Wcm->cm_autolf)
406 curY (tty)++;
407 curX (tty) = 0;
408 break;
409 }
410
411 (void) calccost (tty, curY (tty), curX (tty), row, col, 1);
412 curY (tty) = row, curX (tty) = col;
413 }
414
415
416
417
418
419 void
420 Wcm_clear (struct tty_display_info *tty)
421 {
422 memset (tty->Wcm, 0, sizeof (struct cm));
423 UP = 0;
424 BC = 0;
425 }
426
427
428
429
430
431
432
433
434 int
435 Wcm_init (struct tty_display_info *tty)
436 {
437 #if 0
438 if (tty->Wcm->cm_abs && !tty->Wcm->cm_ds)
439 return 0;
440 #endif
441 if (tty->Wcm->cm_abs)
442 return 0;
443
444 if (!tty->Wcm->cm_up || !tty->Wcm->cm_left)
445 return - 1;
446 if (!tty->Wcm->cm_abs && (!tty->Wcm->cm_down || !tty->Wcm->cm_right))
447 return - 1;
448
449 if (tty->Wcm->cm_rows <= 0 || tty->Wcm->cm_cols <= 0)
450 return - 2;
451 return 0;
452 }