aboutsummaryrefslogtreecommitdiffstats
path: root/src/cm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cm.c')
-rw-r--r--src/cm.c216
1 files changed, 113 insertions, 103 deletions
diff --git a/src/cm.c b/src/cm.c
index da984f0b82d..d520e740eb3 100644
--- a/src/cm.c
+++ b/src/cm.c
@@ -23,8 +23,13 @@ Boston, MA 02110-1301, USA. */
23 23
24#include <config.h> 24#include <config.h>
25#include <stdio.h> 25#include <stdio.h>
26
27#include "lisp.h"
28#include "frame.h"
26#include "cm.h" 29#include "cm.h"
27#include "termhooks.h" 30#include "termhooks.h"
31#include "termchar.h"
32
28 33
29/* For now, don't try to include termcap.h. On some systems, 34/* For now, don't try to include termcap.h. On some systems,
30 configure finds a non-standard termcap.h that the main build 35 configure finds a non-standard termcap.h that the main build
@@ -53,13 +58,16 @@ evalcost (c)
53 return c; 58 return c;
54} 59}
55 60
61/* The terminal to use for low-level output. */
62struct tty_display_info *current_tty;
63
56int 64int
57cmputc (c) 65cmputc (c)
58 char c; 66 char c;
59{ 67{
60 if (termscript) 68 if (current_tty->termscript)
61 fputc (c & 0177, termscript); 69 putc (c & 0177, current_tty->termscript);
62 putchar (c & 0177); 70 putc (c & 0177, current_tty->output);
63 return c; 71 return c;
64} 72}
65 73
@@ -72,9 +80,9 @@ cmputc (c)
72 */ 80 */
73 81
74static 82static
75at (row, col) { 83at (tty, row, col) {
76 curY = row; 84 curY (tty) = row;
77 curX = col; 85 curX (tty) = col;
78} 86}
79 87
80/* 88/*
@@ -82,8 +90,8 @@ at (row, col) {
82 */ 90 */
83 91
84static 92static
85addcol (n) { 93addcol (tty, n) {
86 curX += n; 94 curX (tty) += n;
87 95
88 /* 96 /*
89 * If cursor hit edge of screen, what happened? 97 * If cursor hit edge of screen, what happened?
@@ -93,21 +101,21 @@ addcol (n) {
93 * of the last line. 101 * of the last line.
94 */ 102 */
95 103
96 if (curX == Wcm.cm_cols) { 104 if (curX (tty) == tty->Wcm->cm_cols) {
97 /* 105 /*
98 * Well, if magicwrap, still there, past the edge of the 106 * Well, if magicwrap, still there, past the edge of the
99 * screen (!). If autowrap, on the col 0 of the next line. 107 * screen (!). If autowrap, on the col 0 of the next line.
100 * Otherwise on last column. 108 * Otherwise on last column.
101 */ 109 */
102 110
103 if (Wcm.cm_magicwrap) 111 if (tty->Wcm->cm_magicwrap)
104 ; /* "limbo" */ 112 ; /* "limbo" */
105 else if (Wcm.cm_autowrap) { 113 else if (tty->Wcm->cm_autowrap) {
106 curX = 0; 114 curX (tty) = 0;
107 curY++; /* Beware end of screen! */ 115 curY (tty) ++; /* Beware end of screen! */
108 } 116 }
109 else 117 else
110 curX--; 118 curX (tty)--;
111 } 119 }
112} 120}
113#endif 121#endif
@@ -123,20 +131,20 @@ addcol (n) {
123 * after we reach the last column; this takes us to a known state. 131 * after we reach the last column; this takes us to a known state.
124 */ 132 */
125void 133void
126cmcheckmagic () 134cmcheckmagic (struct tty_display_info *tty)
127{ 135{
128 if (curX == FrameCols) 136 if (curX (tty) == FrameCols (tty))
129 { 137 {
130 if (!MagicWrap || curY >= FrameRows - 1) 138 if (!MagicWrap (tty) || curY (tty) >= FrameRows (tty) - 1)
131 abort (); 139 abort ();
132 if (termscript) 140 if (tty->termscript)
133 putc ('\r', termscript); 141 putc ('\r', tty->termscript);
134 putchar ('\r'); 142 putc ('\r', tty->output);
135 if (termscript) 143 if (tty->termscript)
136 putc ('\n', termscript); 144 putc ('\n', tty->termscript);
137 putchar ('\n'); 145 putc ('\n', tty->output);
138 curX = 0; 146 curX (tty) = 0;
139 curY++; 147 curY (tty)++;
140 } 148 }
141} 149}
142 150
@@ -148,21 +156,21 @@ cmcheckmagic ()
148 */ 156 */
149 157
150void 158void
151cmcostinit () 159cmcostinit (struct tty_display_info *tty)
152{ 160{
153 char *p; 161 char *p;
154 162
155#define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG) 163#define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG)
156#define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e))) 164#define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e)))
157 165
158 Wcm.cc_up = COST (Wcm.cm_up, evalcost); 166 tty->Wcm->cc_up = COST (tty->Wcm->cm_up, evalcost);
159 Wcm.cc_down = COST (Wcm.cm_down, evalcost); 167 tty->Wcm->cc_down = COST (tty->Wcm->cm_down, evalcost);
160 Wcm.cc_left = COST (Wcm.cm_left, evalcost); 168 tty->Wcm->cc_left = COST (tty->Wcm->cm_left, evalcost);
161 Wcm.cc_right = COST (Wcm.cm_right, evalcost); 169 tty->Wcm->cc_right = COST (tty->Wcm->cm_right, evalcost);
162 Wcm.cc_home = COST (Wcm.cm_home, evalcost); 170 tty->Wcm->cc_home = COST (tty->Wcm->cm_home, evalcost);
163 Wcm.cc_cr = COST (Wcm.cm_cr, evalcost); 171 tty->Wcm->cc_cr = COST (tty->Wcm->cm_cr, evalcost);
164 Wcm.cc_ll = COST (Wcm.cm_ll, evalcost); 172 tty->Wcm->cc_ll = COST (tty->Wcm->cm_ll, evalcost);
165 Wcm.cc_tab = Wcm.cm_tabwidth ? COST (Wcm.cm_tab, evalcost) : BIG; 173 tty->Wcm->cc_tab = tty->Wcm->cm_tabwidth ? COST (tty->Wcm->cm_tab, evalcost) : BIG;
166 174
167 /* 175 /*
168 * These last three are actually minimum costs. When (if) they are 176 * These last three are actually minimum costs. When (if) they are
@@ -173,9 +181,9 @@ cmcostinit ()
173 * cursor motion seem to take straight numeric values. --ACT) 181 * cursor motion seem to take straight numeric values. --ACT)
174 */ 182 */
175 183
176 Wcm.cc_abs = CMCOST (Wcm.cm_abs, evalcost); 184 tty->Wcm->cc_abs = CMCOST (tty->Wcm->cm_abs, evalcost);
177 Wcm.cc_habs = CMCOST (Wcm.cm_habs, evalcost); 185 tty->Wcm->cc_habs = CMCOST (tty->Wcm->cm_habs, evalcost);
178 Wcm.cc_vabs = CMCOST (Wcm.cm_vabs, evalcost); 186 tty->Wcm->cc_vabs = CMCOST (tty->Wcm->cm_vabs, evalcost);
179 187
180#undef CMCOST 188#undef CMCOST
181#undef COST 189#undef COST
@@ -188,8 +196,8 @@ cmcostinit ()
188 */ 196 */
189 197
190static int 198static int
191calccost (srcy, srcx, dsty, dstx, doit) 199calccost (struct tty_display_info *tty,
192 int srcy, srcx, dsty, dstx, doit; 200 int srcy, int srcx, int dsty, int dstx, int doit)
193{ 201{
194 register int deltay, 202 register int deltay,
195 deltax, 203 deltax,
@@ -206,16 +214,16 @@ calccost (srcy, srcx, dsty, dstx, doit)
206 don't believe the cursor position: give up here 214 don't believe the cursor position: give up here
207 and force use of absolute positioning. */ 215 and force use of absolute positioning. */
208 216
209 if (curX == Wcm.cm_cols) 217 if (curX (tty) == tty->Wcm->cm_cols)
210 goto fail; 218 goto fail;
211 219
212 totalcost = 0; 220 totalcost = 0;
213 if ((deltay = dsty - srcy) == 0) 221 if ((deltay = dsty - srcy) == 0)
214 goto x; 222 goto x;
215 if (deltay < 0) 223 if (deltay < 0)
216 p = Wcm.cm_up, c = Wcm.cc_up, deltay = -deltay; 224 p = tty->Wcm->cm_up, c = tty->Wcm->cc_up, deltay = -deltay;
217 else 225 else
218 p = Wcm.cm_down, c = Wcm.cc_down; 226 p = tty->Wcm->cm_down, c = tty->Wcm->cc_down;
219 if (c == BIG) { /* caint get thar from here */ 227 if (c == BIG) { /* caint get thar from here */
220 if (doit) 228 if (doit)
221 printf ("OOPS"); 229 printf ("OOPS");
@@ -224,16 +232,16 @@ calccost (srcy, srcx, dsty, dstx, doit)
224 totalcost = c * deltay; 232 totalcost = c * deltay;
225 if (doit) 233 if (doit)
226 while (--deltay >= 0) 234 while (--deltay >= 0)
227 tputs (p, 1, cmputc); 235 emacs_tputs (tty, p, 1, cmputc);
228x: 236x:
229 if ((deltax = dstx - srcx) == 0) 237 if ((deltax = dstx - srcx) == 0)
230 goto done; 238 goto done;
231 if (deltax < 0) { 239 if (deltax < 0) {
232 p = Wcm.cm_left, c = Wcm.cc_left, deltax = -deltax; 240 p = tty->Wcm->cm_left, c = tty->Wcm->cc_left, deltax = -deltax;
233 goto dodelta; /* skip all the tab junk */ 241 goto dodelta; /* skip all the tab junk */
234 } 242 }
235 /* Tabs (the toughie) */ 243 /* Tabs (the toughie) */
236 if (Wcm.cc_tab >= BIG || !Wcm.cm_usetabs) 244 if (tty->Wcm->cc_tab >= BIG || !tty->Wcm->cm_usetabs)
237 goto olddelta; /* forget it! */ 245 goto olddelta; /* forget it! */
238 246
239 /* 247 /*
@@ -244,12 +252,12 @@ x:
244 * we will put into tabx (for ntabs) and tab2x (for n2tabs)). 252 * we will put into tabx (for ntabs) and tab2x (for n2tabs)).
245 */ 253 */
246 254
247 ntabs = (deltax + srcx % Wcm.cm_tabwidth) / Wcm.cm_tabwidth; 255 ntabs = (deltax + srcx % tty->Wcm->cm_tabwidth) / tty->Wcm->cm_tabwidth;
248 n2tabs = ntabs + 1; 256 n2tabs = ntabs + 1;
249 tabx = (srcx / Wcm.cm_tabwidth + ntabs) * Wcm.cm_tabwidth; 257 tabx = (srcx / tty->Wcm->cm_tabwidth + ntabs) * tty->Wcm->cm_tabwidth;
250 tab2x = tabx + Wcm.cm_tabwidth; 258 tab2x = tabx + tty->Wcm->cm_tabwidth;
251 259
252 if (tab2x >= Wcm.cm_cols) /* too far (past edge) */ 260 if (tab2x >= tty->Wcm->cm_cols) /* too far (past edge) */
253 n2tabs = 0; 261 n2tabs = 0;
254 262
255 /* 263 /*
@@ -257,12 +265,12 @@ x:
257 * for using n2tabs, then pick the minimum. 265 * for using n2tabs, then pick the minimum.
258 */ 266 */
259 267
260 /* cost for ntabs + cost for right motion */ 268 /* cost for ntabs + cost for right motion */
261 tabcost = ntabs ? ntabs * Wcm.cc_tab + (dstx - tabx) * Wcm.cc_right 269 tabcost = ntabs ? ntabs * tty->Wcm->cc_tab + (dstx - tabx) * tty->Wcm->cc_right
262 : BIG; 270 : BIG;
263 271
264 /* cost for n2tabs + cost for left motion */ 272 /* cost for n2tabs + cost for left motion */
265 c = n2tabs ? n2tabs * Wcm.cc_tab + (tab2x - dstx) * Wcm.cc_left 273 c = n2tabs ? n2tabs * tty->Wcm->cc_tab + (tab2x - dstx) * tty->Wcm->cc_left
266 : BIG; 274 : BIG;
267 275
268 if (c < tabcost) /* then cheaper to overshoot & back up */ 276 if (c < tabcost) /* then cheaper to overshoot & back up */
@@ -275,11 +283,11 @@ x:
275 * See if tabcost is less than just moving right 283 * See if tabcost is less than just moving right
276 */ 284 */
277 285
278 if (tabcost < (deltax * Wcm.cc_right)) { 286 if (tabcost < (deltax * tty->Wcm->cc_right)) {
279 totalcost += tabcost; /* use the tabs */ 287 totalcost += tabcost; /* use the tabs */
280 if (doit) 288 if (doit)
281 while (--ntabs >= 0) 289 while (--ntabs >= 0)
282 tputs (Wcm.cm_tab, 1, cmputc); 290 emacs_tputs (tty, tty->Wcm->cm_tab, 1, cmputc);
283 srcx = tabx; 291 srcx = tabx;
284 } 292 }
285 293
@@ -292,9 +300,9 @@ newdelta:
292 goto done; 300 goto done;
293olddelta: 301olddelta:
294 if (deltax > 0) 302 if (deltax > 0)
295 p = Wcm.cm_right, c = Wcm.cc_right; 303 p = tty->Wcm->cm_right, c = tty->Wcm->cc_right;
296 else 304 else
297 p = Wcm.cm_left, c = Wcm.cc_left, deltax = -deltax; 305 p = tty->Wcm->cm_left, c = tty->Wcm->cc_left, deltax = -deltax;
298 306
299dodelta: 307dodelta:
300 if (c == BIG) { /* caint get thar from here */ 308 if (c == BIG) { /* caint get thar from here */
@@ -306,7 +314,7 @@ fail:
306 totalcost += c * deltax; 314 totalcost += c * deltax;
307 if (doit) 315 if (doit)
308 while (--deltax >= 0) 316 while (--deltax >= 0)
309 tputs (p, 1, cmputc); 317 emacs_tputs (tty, p, 1, cmputc);
310done: 318done:
311 return totalcost; 319 return totalcost;
312} 320}
@@ -324,7 +332,8 @@ losecursor ()
324#define USECR 3 332#define USECR 3
325 333
326void 334void
327cmgoto (row, col) 335cmgoto (tty, row, col)
336 struct tty_display_info *tty;
328 int row, col; 337 int row, col;
329{ 338{
330 int homecost, 339 int homecost,
@@ -337,47 +346,47 @@ cmgoto (row, col)
337 *dcm; 346 *dcm;
338 347
339 /* First the degenerate case */ 348 /* First the degenerate case */
340 if (row == curY && col == curX) /* already there */ 349 if (row == curY (tty) && col == curX (tty)) /* already there */
341 return; 350 return;
342 351
343 if (curY >= 0 && curX >= 0) 352 if (curY (tty) >= 0 && curX (tty) >= 0)
344 { 353 {
345 /* We may have quick ways to go to the upper-left, bottom-left, 354 /* We may have quick ways to go to the upper-left, bottom-left,
346 * start-of-line, or start-of-next-line. Or it might be best to 355 * start-of-line, or start-of-next-line. Or it might be best to
347 * start where we are. Examine the options, and pick the cheapest. 356 * start where we are. Examine the options, and pick the cheapest.
348 */ 357 */
349 358
350 relcost = calccost (curY, curX, row, col, 0); 359 relcost = calccost (tty, curY (tty), curX (tty), row, col, 0);
351 use = USEREL; 360 use = USEREL;
352 if ((homecost = Wcm.cc_home) < BIG) 361 if ((homecost = tty->Wcm->cc_home) < BIG)
353 homecost += calccost (0, 0, row, col, 0); 362 homecost += calccost (tty, 0, 0, row, col, 0);
354 if (homecost < relcost) 363 if (homecost < relcost)
355 relcost = homecost, use = USEHOME; 364 relcost = homecost, use = USEHOME;
356 if ((llcost = Wcm.cc_ll) < BIG) 365 if ((llcost = tty->Wcm->cc_ll) < BIG)
357 llcost += calccost (Wcm.cm_rows - 1, 0, row, col, 0); 366 llcost += calccost (tty, tty->Wcm->cm_rows - 1, 0, row, col, 0);
358 if (llcost < relcost) 367 if (llcost < relcost)
359 relcost = llcost, use = USELL; 368 relcost = llcost, use = USELL;
360 if ((crcost = Wcm.cc_cr) < BIG) { 369 if ((crcost = tty->Wcm->cc_cr) < BIG) {
361 if (Wcm.cm_autolf) 370 if (tty->Wcm->cm_autolf)
362 if (curY + 1 >= Wcm.cm_rows) 371 if (curY (tty) + 1 >= tty->Wcm->cm_rows)
363 crcost = BIG; 372 crcost = BIG;
364 else 373 else
365 crcost += calccost (curY + 1, 0, row, col, 0); 374 crcost += calccost (tty, curY (tty) + 1, 0, row, col, 0);
366 else 375 else
367 crcost += calccost (curY, 0, row, col, 0); 376 crcost += calccost (tty, curY (tty), 0, row, col, 0);
368 } 377 }
369 if (crcost < relcost) 378 if (crcost < relcost)
370 relcost = crcost, use = USECR; 379 relcost = crcost, use = USECR;
371 directcost = Wcm.cc_abs, dcm = Wcm.cm_abs; 380 directcost = tty->Wcm->cc_abs, dcm = tty->Wcm->cm_abs;
372 if (row == curY && Wcm.cc_habs < BIG) 381 if (row == curY (tty) && tty->Wcm->cc_habs < BIG)
373 directcost = Wcm.cc_habs, dcm = Wcm.cm_habs; 382 directcost = tty->Wcm->cc_habs, dcm = tty->Wcm->cm_habs;
374 else if (col == curX && Wcm.cc_vabs < BIG) 383 else if (col == curX (tty) && tty->Wcm->cc_vabs < BIG)
375 directcost = Wcm.cc_vabs, dcm = Wcm.cm_vabs; 384 directcost = tty->Wcm->cc_vabs, dcm = tty->Wcm->cm_vabs;
376 } 385 }
377 else 386 else
378 { 387 {
379 directcost = 0, relcost = 100000; 388 directcost = 0, relcost = 100000;
380 dcm = Wcm.cm_abs; 389 dcm = tty->Wcm->cm_abs;
381 } 390 }
382 391
383 /* 392 /*
@@ -388,13 +397,14 @@ cmgoto (row, col)
388 { 397 {
389 /* compute REAL direct cost */ 398 /* compute REAL direct cost */
390 cost = 0; 399 cost = 0;
391 p = dcm == Wcm.cm_habs ? tgoto (dcm, row, col) : 400 p = (dcm == tty->Wcm->cm_habs
392 tgoto (dcm, col, row); 401 ? tgoto (dcm, row, col)
393 tputs (p, 1, evalcost); 402 : tgoto (dcm, col, row));
403 emacs_tputs (tty, p, 1, evalcost);
394 if (cost <= relcost) 404 if (cost <= relcost)
395 { /* really is cheaper */ 405 { /* really is cheaper */
396 tputs (p, 1, cmputc); 406 emacs_tputs (tty, p, 1, cmputc);
397 curY = row, curX = col; 407 curY (tty) = row, curX (tty) = col;
398 return; 408 return;
399 } 409 }
400 } 410 }
@@ -402,25 +412,25 @@ cmgoto (row, col)
402 switch (use) 412 switch (use)
403 { 413 {
404 case USEHOME: 414 case USEHOME:
405 tputs (Wcm.cm_home, 1, cmputc); 415 emacs_tputs (tty, tty->Wcm->cm_home, 1, cmputc);
406 curY = 0, curX = 0; 416 curY (tty) = 0, curX (tty) = 0;
407 break; 417 break;
408 418
409 case USELL: 419 case USELL:
410 tputs (Wcm.cm_ll, 1, cmputc); 420 emacs_tputs (tty, tty->Wcm->cm_ll, 1, cmputc);
411 curY = Wcm.cm_rows - 1, curX = 0; 421 curY (tty) = tty->Wcm->cm_rows - 1, curX (tty) = 0;
412 break; 422 break;
413 423
414 case USECR: 424 case USECR:
415 tputs (Wcm.cm_cr, 1, cmputc); 425 emacs_tputs (tty, tty->Wcm->cm_cr, 1, cmputc);
416 if (Wcm.cm_autolf) 426 if (tty->Wcm->cm_autolf)
417 curY++; 427 curY (tty)++;
418 curX = 0; 428 curX (tty) = 0;
419 break; 429 break;
420 } 430 }
421 431
422 (void) calccost (curY, curX, row, col, 1); 432 (void) calccost (tty, curY (tty), curX (tty), row, col, 1);
423 curY = row, curX = col; 433 curY (tty) = row, curX (tty) = col;
424} 434}
425 435
426/* Clear out all terminal info. 436/* Clear out all terminal info.
@@ -428,9 +438,9 @@ cmgoto (row, col)
428 */ 438 */
429 439
430void 440void
431Wcm_clear () 441Wcm_clear (struct tty_display_info *tty)
432{ 442{
433 bzero (&Wcm, sizeof Wcm); 443 bzero (tty->Wcm, sizeof (struct cm));
434 UP = 0; 444 UP = 0;
435 BC = 0; 445 BC = 0;
436} 446}
@@ -443,21 +453,21 @@ Wcm_clear ()
443 */ 453 */
444 454
445int 455int
446Wcm_init () 456Wcm_init (struct tty_display_info *tty)
447{ 457{
448#if 0 458#if 0
449 if (Wcm.cm_abs && !Wcm.cm_ds) 459 if (tty->Wcm->cm_abs && !tty->Wcm->cm_ds)
450 return 0; 460 return 0;
451#endif 461#endif
452 if (Wcm.cm_abs) 462 if (tty->Wcm->cm_abs)
453 return 0; 463 return 0;
454 /* Require up and left, and, if no absolute, down and right */ 464 /* Require up and left, and, if no absolute, down and right */
455 if (!Wcm.cm_up || !Wcm.cm_left) 465 if (!tty->Wcm->cm_up || !tty->Wcm->cm_left)
456 return - 1; 466 return - 1;
457 if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right)) 467 if (!tty->Wcm->cm_abs && (!tty->Wcm->cm_down || !tty->Wcm->cm_right))
458 return - 1; 468 return - 1;
459 /* Check that we know the size of the screen.... */ 469 /* Check that we know the size of the screen.... */
460 if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0) 470 if (tty->Wcm->cm_rows <= 0 || tty->Wcm->cm_cols <= 0)
461 return - 2; 471 return - 2;
462 return 0; 472 return 0;
463} 473}