Debian Codegraph @

screen.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9 #include "fp.h"
10 #include "chars.h"
11 #include "jctype.h"
12 #include "disp.h"
13 #include "extend.h"
14 #include "fmt.h"
15 #include "term.h"
16 #include "mac.h"
17 #include "screen.h"
18 #include "wind.h"
19
20 int AbortCnt,
21 tabstop = 8; /* VAR: expand tabs to this number of spaces */
22
23 struct scrimage
24 *DesiredScreen = NULL,
25 *PhysScreen = NULL;
26
27 private struct screenline *Savelines = NULL; /* scratch entries (LI of them) */
28
29 private void LEclear(struct screenline *); /* free s_effects component */
30
31 private char *cursor; /* offset into current Line */
32
33 char *cursend;
34
35 /* Position in tercap screen. INFINITY means "don't know". */
36 int CapCol,
37 CapLine;
38
39 private int
40 i_line,
41 i_col;
42
43 void
44 make_scr(void)
45 {
46 int i;
47 struct screenline
48 *ns;
49 char *nsp;
50 static char
51 *screenchars = NULL;
52 static volatile int
53 oldLI = 0;
54
55 /* In case we are RESHAPING the window! */
56 if (DesiredScreen != NULL)
57 free(DesiredScreen);
58 if (PhysScreen != NULL)
59 free(PhysScreen);
60 i = oldLI;
61 oldLI = 0;
62 if (Savelines != NULL) {
63 /* Note: each screenline in Savelines has a null s_effects
64 * (or is uninitialized). LEclear must not be applied.
65 */
66 free(Savelines);
67 }
68 if (Screen != NULL) {
69 #ifdef HIGHLIGHTING
70 for (ns = Screen; ns != &Screen[i]; ns++)
71 LEclear(ns);
72 #endif
73 free(Screen);
74 }
75 if (screenchars != NULL)
76 free(screenchars); /* free all the screen data */
77
78 DesiredScreen = (struct scrimage *) malloc((unsigned) LI * sizeof (struct scrimage));
79 PhysScreen = (struct scrimage *) malloc((unsigned) LI * sizeof (struct scrimage));
80
81 Savelines = (struct screenline *)
82 malloc((unsigned) LI * sizeof(struct screenline));
83 ns = Screen = (struct screenline *)
84 malloc((unsigned) LI * sizeof(struct screenline));
85
86 nsp = screenchars = (char *) malloc((size_t)CO * LI);
87
88 if (DesiredScreen == NULL
89 || PhysScreen == NULL
90 || Savelines == NULL
91 || ns == NULL
92 || nsp == NULL)
93 {
94 writef("\n\rCannot malloc screen!\n");
95 finish(-1); /* die! */
96 }
97
98 for (i = 0; i < LI; i++) {
99 ns->s_line = nsp;
100 /* End of Line (nsp[CO-1] is never used) */
101 ns->s_roof = nsp + CO - 1;
102 ns->s_effects = NOEFFECT;
103 nsp += CO;
104 ns += 1;
105
106 /* ??? The following is a fudge to placate Purify.
107 * There is a real bug here, so we squash it with
108 * a sledge hammer. What is the correct fix?
109 */
110 {
111 struct scrimage *p;
112
113 p = &PhysScreen[i];
114 p->s_offset = 0;
115 p->s_flags = 0;
116 p->s_vln = 0;
117 p->s_id = NULL_DADDR;
118 p->s_lp = NULL;
119 p->s_window = NULL;
120
121 p = &DesiredScreen[i];
122 p->s_offset = 0;
123 p->s_flags = 0;
124 p->s_vln = 0;
125 p->s_id = NULL_DADDR;
126 p->s_lp = NULL;
127 p->s_window = NULL;
128 }
129 }
130 oldLI = LI;
131 SO_off();
132 #ifdef HIGHLIGHTING
133 US_effect(NO);
134 #endif
135 cl_scr(NO);
136 }
137
138 void
139 clrline(char *cp1, char *cp2)
140 {
141 while (cp1 < cp2)
142 *cp1++ = ' ';
143 }
144
145
146 /* Output one character (if necessary) at the current position */
147
148 #ifdef MAC
149
150 /* Character output to bit-mapped screen is very expensive. It makes
151 * much more sense to write the entire line at once. So, we print all
152 * the characters, whether already there or not, once the line is
153 * complete.
154 */
155
156 private unsigned char sput_buf[255];
157 private size_t sput_len = 0;
158
159 private void
160 sput_start(void)
161 {
162 /* if (i_line != CapLine || i_col != CapCol) */
163 NPlacur(i_line, i_col);
164 sput_len = 0;
165 }
166
167 private void
168 sput_end(void)
169 {
170 if (sput_len != 0) {
171 writetext(sput_buf, sput_len);
172 sput_len = 0;
173 }
174 }
175
176 private void
177 sputc(char c)
178 {
179 /* if line gets too long for sput_buf, ignore subsequent chars */
180 if (sput_len < sizeof(sput_buf)) {
181 *cursor++ = c;
182 sput_buf[sput_len++] = (c == '0')? 0xAF /* slashed zero */ : c;
183 CapCol++;
184 i_col++;
185 }
186 }
187
188 #else /* !MAC */
189
190 # ifdef HIGHLIGHTING
191 # define CharChanged(c) (*cursor != (char) (c))
192 # else /* !HIGHLIGHTING */
193 private jbool ChangeEffect = NO;
194 # define CharChanged(c) (ChangeEffect || *cursor != (char) (c))
195 # endif /* !HIGHLIGHTING */
196
197 # ifdef IBMPCDOS
198 /* On PC, we think that trying to avoid painting the character
199 * is slower than just doing it. I wonder if this is true.
200 */
201 # define sputc(c) do_sputc(c)
202 # else /* !IBMPCDOS */
203 # define sputc(c) { \
204 if (CharChanged(c)) { \
205 do_sputc(c); \
206 } else { \
207 cursor += 1; \
208 i_col += 1; \
209 } \
210 }
211 # endif /* !IBMPCDOS */
212
213 private void
214 do_sputc(char c)
215 {
216 if (CharChanged(c)) {
217 # ifdef ID_CHAR
218 INSmode(NO);
219 # endif
220 if (i_line != CapLine || i_col != CapCol)
221 Placur(i_line, i_col);
222 *cursor++ = c;
223 # ifdef TERMCAP
224 if (UL && c == '_' && *cursor != ' ')
225 putstr(" \b"); /* Erase so '_' looks right. */
226 # endif
227 scr_putchar(c);
228 AbortCnt -= 1;
229 CapCol += 1;
230 } else {
231 cursor += 1;
232 }
233 i_col += 1;
234 }
235 #endif /* !MAC */
236
237 #ifdef HIGHLIGHTING
238
239 private void (*real_effect)(jbool);
240
241 private void
242 do_hlsputc(const struct LErange *hl, /* desired highlighting */
243 const struct LErange *oldhl, /* previous highlighting */
244 char c)
245 {
246 /* assert: hl != NULL && oldhl != NULL
247 * In other words, hl and oldhl must point to real LErange structs.
248 */
249
250 /* The following two initializing expressions use the peculiar
251 * properties of unsigneds to make an efficient range test.
252 */
253 void
254 (*virtual_effect)(jbool) =
255 (unsigned)i_col - hl->start < hl->width? hl->high : hl->norm,
256 (*underlying_effect)(jbool) =
257 (unsigned)i_col - oldhl->start < oldhl->width? oldhl->high : oldhl->norm;
258
259 if (*cursor != c || virtual_effect != underlying_effect) {
260 # ifdef ID_CHAR
261 INSmode(NO);
262 # endif
263 if (i_line != CapLine || i_col != CapCol)
264 Placur(i_line, i_col);
265 if (virtual_effect != real_effect) {
266 if (real_effect != NULL)
267 real_effect(NO);
268 /* instantaneously in neutral state */
269 if (virtual_effect != NULL)
270 virtual_effect(YES);
271 real_effect = virtual_effect;
272 }
273 # ifdef TERMCAP
274 if (UL && c == '_' && *cursor != ' ')
275 putstr(" \b"); /* Erase so '_' looks right. */
276 # endif
277 *cursor++ = c;
278 scr_putchar(c);
279 AbortCnt -= 1;
280 CapCol += 1;
281 } else {
282 cursor += 1;
283 }
284 i_col += 1;
285 }
286
287 #endif /* HIGHLIGHTING */
288
289 void
290 cl_eol(void)
291 {
292 if (cursor == Curline->s_line)
293 LEclear(Curline); /* in case swrite was not called (hack!) */
294
295 if (cursor < Curline->s_roof) {
296 #ifdef TERMCAP
297 if (CE) {
298 Placur(i_line, i_col);
299 putpad(CE, 1);
300 clrline(cursor, Curline->s_roof);
301 } else {
302 /* Ugh. The slow way for dumb terminals. */
303 char *savecp = cursor;
304
305 while (cursor < Curline->s_roof)
306 sputc(' ');
307 cursor = savecp;
308 }
309 #else /* !TERMCAP */
310 Placur(i_line, i_col);
311 clr_eoln(); /* MAC and PCSCR define this */
312 clrline(cursor, Curline->s_roof);
313 #endif /* !TERMCAP */
314 Curline->s_roof = cursor;
315 }
316 }
317
318 void
319 cl_scr(jbool doit)
320 {
321 int i;
322 struct screenline
323 *sp = Screen;
324
325 for (i = 0; i < LI; i++, sp++) {
326 LEclear(sp);
327 clrline(sp->s_line, sp->s_roof);
328 sp->s_roof = sp->s_line;
329 PhysScreen[i].s_id = NULL_DADDR;
330 }
331 if (doit) {
332 clr_page();
333 CapCol = CapLine = 0;
334 UpdMesg = YES;
335 }
336 }
337
338 /* routines to manage a pool of LErange structs */
339
340 #ifdef HIGHLIGHTING
341
342 union LEspace {
343 struct LErange le;
344 union LEspace *next;
345 };
346
347 private union LEspace *LEfreeHead = NULL;
348
349 private struct LErange *
350 LEnew(void)
351 {
352 struct LErange *ret;
353
354 if (LEfreeHead == NULL) {
355 LEfreeHead = (union LEspace *) emalloc(sizeof(union LEspace));
356 LEfreeHead->next = NULL;
357 }
358 ret = &LEfreeHead->le;
359 LEfreeHead = LEfreeHead->next;
360 return ret;
361 }
362
363 #endif /* HIGHLIGHTING */
364
365 private void
366 LEclear(struct screenline *sl)
367 {
368 #ifdef HIGHLIGHTING
369 if (sl->s_effects != NOEFFECT) {
370 ((union LEspace *) sl->s_effects)->next = LEfreeHead;
371 LEfreeHead = (union LEspace *) sl->s_effects;
372 }
373 #endif /* HIGHLIGHTING */
374 sl->s_effects = NOEFFECT;
375 }
376
377 /* Write `line' at the current position of `cursor'. Stop when we
378 * reach the end of the screen. Aborts if there is a character
379 * waiting.
380 *
381 * Note: All callers must have "DeTabed" "line", or processed
382 * it equivalently -- it is presumed that line contains only
383 * displayable characters.
384 */
385
386 jbool
387 swrite(char *line, LineEffects hl, jbool abortable)
388 {
389 int n = cursend - cursor;
390 jbool aborted = NO;
391 /* Unfortunately, neither of our LineEffects representation
392 * is suitable for representing the state of a partially
393 * updated line. Consequently, this routine unconditionally
394 * replaces the old hl with the new. To ensure that the new
395 * hl is correct, we compute MinCol to indicate how far in
396 * the line we must get, and will not abort until we have
397 * reached at least that column.
398 *
399 * This is unacceptably ugly. We really must switch to a better
400 * representation
401 */
402 int MinCol = 0;
403 #ifdef HIGHLIGHTING
404 /* If either the old line or the new line has effects,
405 * we know that some effects processing is necessary.
406 * If so, we ensure that the old line has an effect
407 * by adding a no-op effect if necessary: this is needed
408 * to ensure Placur does not get into trouble. (UGLY!)
409 */
410 struct LErange *oldhl = Curline->s_effects;
411 static const struct LErange nohl = { 0, 0, LENULLPROC, LENULLPROC };
412
413 if (oldhl != NOEFFECT) {
414 int w = Curline->s_roof - Curline->s_line;
415
416 if (oldhl->norm != NULL)
417 MinCol = w;
418 if (oldhl->high != NULL && w > (int)oldhl->start)
419 MinCol = jmax(MinCol, jmin(w, (int) (oldhl->start + oldhl->width)));
420 }
421 /* If either the old line or the new line has effects,
422 * we know that some effects processing is necessary.
423 * If so, we ensure that the old line has an effect
424 * by adding a no-op effect if necessary: this is needed
425 * to ensure Placur does not get into trouble. (UGLY!)
426 */
427 if (hl != NOEFFECT) {
428 if (hl->high != NULL)
429 MinCol = jmax(MinCol, (int) (hl->start + hl->width));
430 if (oldhl == NOEFFECT) {
431 oldhl = Curline->s_effects = LEnew(); /* keep Placur on-track */
432 *oldhl = nohl;
433 }
434 }
435 real_effect = LENULLPROC;
436 #else /* !HIGHLIGHTING */
437 if (Curline->s_effects != hl)
438 MinCol = Curline->s_roof - Curline->s_line; /* must obliterate old */
439 #endif /* !HIGHLIGHTING */
440
441 if (n > 0) {
442 ZXchar c;
443
444 #ifdef HIGHLIGHTING
445 /* nnhl: non-NULL version of hl (possibly
446 * a no-op) to reduce the cases handled.
447 */
448 const struct LErange *nnhl = hl == NOEFFECT? &nohl : hl;
449 # define spit(c) { if (oldhl != NULL) do_hlsputc(nnhl,oldhl,c); else sputc(c); }
450
451 #else /* !HIGHLIGHTING */
452
453 # define spit(c) sputc(c)
454
455 # ifdef MAC
456 sput_start(); /* Okay, because no interruption possible */
457 # else /* !MAC */
458 if (hl != Curline->s_effects)
459 ChangeEffect = YES;
460 # endif /* !MAC */
461
462 if (hl != NOEFFECT)
463 SO_effect(YES);
464 #endif /* !HIGHLIGHTING */
465
466 while ((c = ZXC(*line++)) != '\0') {
467 if (abortable && i_col >= MinCol && AbortCnt < 0) {
468 AbortCnt = ScrBufSize;
469 if (PreEmptOutput()) {
470 aborted = YES;
471 break;
472 }
473 }
474 #ifdef TERMCAP
475 if (Hazeltine && c == '~')
476 c = '`';
477 #endif
478 #ifdef CODEPAGE437
479 /* ??? Some archane mapping of IBM PC characters.
480 * According to the appendix of the Microsoft MSDOS
481 * Operating System 5.0 User's Guide and Reference,
482 * in Code Page 437 (USA English) ' ', 0x00, and 0xFF are
483 * blank and 0x01 is a face.
484 */
485 if (c == 0xFF)
486 c = 1;
487 else if (c == ' ' && hl != NOEFFECT)
488 c = 0xFF;
489 #endif /* CODEPAGE437 */
490 if (--n <= 0) {
491 /* We've got one more column -- how will we spend it?
492 * ??? This is probably redundant -- callers do truncation.
493 */
494 if (*line != '\0')
495 c = '!';
496 spit(c);
497 break;
498 }
499 spit(c);
500 }
501 #ifdef HIGHLIGHTING
502 if (real_effect != NULL)
503 real_effect(NO);
504 #else /* !HIGHLIGHTING */
505 # ifdef MAC
506 sput_end(); /* flush before reverting SO */
507 # else /* !MAC */
508 ChangeEffect = NO;
509 # endif /* !MAC */
510 if (hl != NOEFFECT)
511 SO_off();
512 #endif /* !HIGHLIGHTING */
513 if (cursor > Curline->s_roof)
514 Curline->s_roof = cursor;
515 # undef spit
516 }
517 #ifdef HIGHLIGHTING
518 if (hl == NOEFFECT)
519 LEclear(Curline);
520 else
521 *(Curline->s_effects) = *hl;
522 #else /* !HIGHLIGHTING */
523 Curline->s_effects = hl;
524 #endif /* !HIGHLIGHTING */
525 return !aborted;
526 }
527
528 void
529 i_set(int nline, int ncol)
530 {
531 Curline = &Screen[nline];
532 cursor = Curline->s_line + ncol;
533 cursend = &Curline->s_line[CO - 1];
534 i_line = nline;
535 i_col = ncol;
536 }
537
538 void
539 SO_off(void)
540 {
541 SO_effect(NO);
542 }
543
544 #ifdef TERMCAP
545
546 void
547 SO_effect(jbool on)
548 {
549 /* If there are magic cookies, then WHERE the SO string is
550 * printed decides where the SO actually starts on the screen.
551 * So it's important to make sure the cursor is positioned there
552 * anyway. I think this is right.
553 */
554 if (SG != 0) {
555 Placur(i_line, i_col);
556 i_col += SG;
557 CapCol += SG;
558 cursor += SG;
559 }
560 putpad(on? SO : SE, 1);
561 }
562
563 # ifdef HIGHLIGHTING
564 void
565 US_effect(jbool on)
566 {
567 if (UG == 0) /* not used if magic cookies */
568 putpad(on? US : UE, 1);
569 }
570 # endif /* HIGHLIGHTING */
571
572 #endif /* TERMCAP */
573
574 /* Insert `num' lines at top, but leave all the lines BELOW `bottom'
575 * alone (at least they won't look any different when we are done).
576 * This changes the screen array AND does the physical changes.
577 */
578 void
579 v_ins_line(int num, int top, int bottom)
580 {
581 int i;
582
583 /* assert(num <= bottom-top+1) */
584
585 /* Blank and save the screen pointers that will fall off the end. */
586
587 for(i = 0; i < num; i++) {
588 struct screenline *sp = &Screen[bottom - i];
589
590 clrline(sp->s_line, sp->s_roof);
591 sp->s_roof = sp->s_line;
592 LEclear(sp);
593 Savelines[i] = *sp;
594 }
595
596 /* Num number of bottom lines will be lost.
597 * Copy everything down num number of times.
598 */
599 for (i = bottom-num; i >= top; i--)
600 Screen[i + num] = Screen[i];
601
602 /* Insert the now-blank saved ones at the top. */
603
604 for (i = 0; i < num; i++)
605 Screen[top + i] = Savelines[i];
606 i_lines(top, bottom, num);
607 }
608
609 /* Delete `num' lines starting at `top' leaving the lines below `bottom'
610 * alone. This updates the internal image as well as the physical image.
611 */
612 void
613 v_del_line(int num, int top, int bottom)
614 {
615 int i;
616
617 /* assert(num <= bottom-top+1) */
618
619 /* Blank and save the lines to be deleted from the top. */
620
621 for (i = 0; i < num; i++) {
622 struct screenline *sp = &Screen[top + i];
623
624 clrline(sp->s_line, sp->s_roof);
625 sp->s_roof = sp->s_line;
626 LEclear(sp);
627 Savelines[i] = *sp;
628 }
629
630 /* Copy everything up num number of lines. */
631
632 for (i = top; i + num <= bottom; i++)
633 Screen[i] = Screen[i + num];
634
635 /* Restore the now-blank lost lines */
636
637 for (i = 0; i < num; i++)
638 Screen[bottom - i] = Savelines[i];
639 d_lines(top, bottom, num);
640 }
641
642 #ifdef TERMCAP /* remainder of this file */
643
644 /* The cursor optimization happens here. You may decide that this
645 * is going too far with cursor optimization, or perhaps it should
646 * limit the amount of checking to when the output speed is slow.
647 * What ever turns you on ...
648 */
649 struct cursaddr {
650 int cm_numchars;
651 void (*cm_proc)(int, int);
652 };
653
654 private char *Cmstr;
655 private struct cursaddr *HorMin,
656 *VertMin,
657 *DirectMin;
658
659 private void
660 ForTab(int, int),
661 RetTab(int, int),
662 DownMotion(int, int),
663 UpMotion(int, int),
664 GoDirect(int, int),
665 HomeGo(int, int),
666 BottomUp(int, int);
667
668
669 private struct cursaddr WarpHor[] = {
670 { 0, ForTab },
671 { 0, RetTab }
672 };
673
674 private struct cursaddr WarpVert[] = {
675 { 0, DownMotion },
676 { 0, UpMotion }
677 };
678
679 private struct cursaddr WarpDirect[] = {
680 { 0, GoDirect },
681 { 0, HomeGo },
682 { 0, BottomUp }
683 };
684
685 # define FORTAB 0 /* Forward using tabs */
686 # define RETFORTAB 1 /* Beginning of line and then tabs */
687 # define NUMHOR 2
688
689 # define DOWN 0 /* Move down */
690 # define UPMOVE 1 /* Move up */
691 # define NUMVERT 2
692
693 # define DIRECT 0 /* Using CM */
694 # define HOME 1 /* HOME */
695 # define LOWER 2 /* Lower Line */
696 # define NUMDIRECT 3
697
698 # define home() Placur(0, 0)
699 # define LowLine() { putpad(LL, 1); CapLine = ILI; CapCol = 0; }
700 # define PrintHo() { putpad(HO, 1); CapLine = CapCol = 0; }
701
702 private void
703 GoDirect(int line, int col)
704 {
705 putpad(Cmstr, 1);
706 CapLine = line;
707 CapCol = col;
708 }
709
710 private void
711 RetTab(int UNUSED(line), int col)
712 {
713 scr_putchar('\r');
714 CapCol = 0;
715 ForTab(-1, col);
716 }
717
718 private void
719 HomeGo(int line, int col)
720 {
721 PrintHo();
722 DownMotion(line, -1);
723 ForTab(-1, col);
724 }
725
726 private void
727 BottomUp(int line, int col)
728 {
729 LowLine();
730 UpMotion(line, -1);
731 ForTab(-1, col);
732 }
733
734 /* Tries to move forward using tabs (if possible). It tabs to the
735 * closest tabstop which means it may go past 'destcol' and backspace
736 * to it.
737 * Note: changes to this routine must be matched by changes in ForNum.
738 */
739 private void
740 ForTab(int UNUSED(line), int col)
741 {
742 if ((col > CapCol+1) && TABS && (phystab > 0)) {
743 int tabgoal,
744 ntabs,
745 pts = phystab;
746
747 tabgoal = col + (pts / 2);
748 tabgoal -= (tabgoal % pts);
749
750 /* Don't tab to last place or else it is likely to screw up. */
751 if (tabgoal >= CO)
752 tabgoal -= pts;
753
754 ntabs = (tabgoal / pts) - (CapCol / pts);
755 /* If tabbing moves past goal, and goal is more cols back
756 * than we would have had to move forward from our original
757 * position, tab is counterproductive. Notice that if our
758 * original motion would have been backwards, tab loses too,
759 * so we need not write abs(to-CapCol).
760 */
761 if (tabgoal > col && tabgoal-col >= col-CapCol)
762 ntabs = 0;
763 while (--ntabs >= 0) {
764 scr_putchar('\t');
765 CapCol = tabgoal; /* idempotent */
766 }
767 }
768
769 if (col > CapCol) {
770 char *cp = &Screen[CapLine].s_line[CapCol];
771
772 # ifdef ID_CHAR
773 INSmode(NO); /* we're not just a motion */
774 # endif
775 while (col > CapCol) {
776 scr_putchar(*cp++);
777 CapCol++;
778 }
779 }
780
781 while (col < CapCol) {
782 putpad(BC, 1);
783 CapCol--;
784 }
785 }
786
787 private void
788 DownMotion(int line, int UNUSED(col))
789 {
790 int nlines = line - CapLine;
791
792 while (--nlines >= 0) {
793 putpad(DO, 1);
794 CapLine = line; /* idempotent */
795 }
796 }
797
798 private void
799 UpMotion(int line, int UNUSED(col))
800 {
801 int nchars = CapLine - line;
802
803 while (--nchars >= 0) {
804 putpad(UP, 1);
805 CapLine = line; /* idempotent */
806 }
807 }
808
809 private int ForNum(int from, int to);
810
811 void
812 Placur(int line, int col)
813 {
814 # define CursMin(which,addrs,max) { \
815 int best = 0, \
816 i; \
817 struct cursaddr *cp; \
818 for (cp = &(addrs)[1], i = 1; i < (max); i++, cp++) \
819 if (cp->cm_numchars < (addrs)[best].cm_numchars) \
820 best = i; \
821 (which) = &(addrs)[best]; \
822 }
823
824 if (line == CapLine && col == CapCol)
825 return; /* We are already there. */
826
827 /* Number of characters to move horizontally for each case.
828 * 1: Try tabbing to the correct place.
829 * 2: Try going to the beginning of the line, and then tab.
830 */
831 {
832 int dcol = col - CapCol; /* Number of columns to move */
833 int xtracost = 0; /* Misc addition to cost. */
834
835 # ifdef ID_CHAR
836 if (IN_INSmode && MI)
837 xtracost = IMEIlen;
838 /* If we're already in insert mode, it is likely that we will
839 * want to be in insert mode again, after the insert.
840 */
841 # endif
842
843 if (dcol == 1 || dcol == 0) { /* Most common case. */
844 HorMin = &WarpHor[FORTAB];
845 HorMin->cm_numchars = dcol + xtracost;
846 } else {
847 /* if CapCol is unknown, FORTAB is impossible */
848 WarpHor[FORTAB].cm_numchars = CapCol == INFINITY? INFINITY :
849 xtracost + ForNum(CapCol, col);
850 WarpHor[RETFORTAB].cm_numchars = xtracost + 1 + ForNum(0, col);
851
852 /* Which is the shortest of the bunch */
853
854 CursMin(HorMin, WarpHor, NUMHOR);
855 }
856 }
857
858 /* Moving vertically is more simple. */
859 {
860 int dline = line - CapLine; /* Number of lines to move */
861
862 WarpVert[DOWN].cm_numchars = dline >= 0 ? dline : INFINITY;
863 WarpVert[UPMOVE].cm_numchars = dline < 0 ? ((-dline) * UPlen) : INFINITY;
864 }
865
866 /* Which of these is simpler */
867 CursMin(VertMin, WarpVert, NUMVERT);
868
869 /* Homing first and lowering first are considered
870 * direct motions.
871 * Homing first's total is the sum of the cost of homing
872 * and the sum of tabbing (if possible) to the right.
873 */
874 if (Screen[line].s_effects != NOEFFECT && CM != NULL) {
875 /* We are going to a line with inversion or underlining;
876 * Don't try any clever stuff
877 */
878 DirectMin = &WarpDirect[DIRECT];
879 DirectMin->cm_numchars = 0;
880 Cmstr = targ2(CM, col, line);
881 } else if (VertMin->cm_numchars + HorMin->cm_numchars <= 3) {
882 /* Since no direct method is ever shorter than 3 chars, don't try it. */
883 DirectMin = &WarpDirect[DIRECT]; /* A dummy ... */
884 DirectMin->cm_numchars = INFINITY;
885 } else {
886 WarpDirect[DIRECT].cm_numchars = CM != NULL ?
887 strlen(Cmstr = targ2(CM, col, line)) : INFINITY;
888 WarpDirect[HOME].cm_numchars = HOlen + line +
889 WarpHor[RETFORTAB].cm_numchars;
890 WarpDirect[LOWER].cm_numchars = LLlen + ((ILI - line) * UPlen) +
891 WarpHor[RETFORTAB].cm_numchars;
892 CursMin(DirectMin, WarpDirect, NUMDIRECT);
893 }
894
895 if (HorMin->cm_numchars + VertMin->cm_numchars < DirectMin->cm_numchars) {
896 if (line != CapLine)
897 (VertMin->cm_proc)(line, -1);
898 if (col != CapCol) {
899 # ifdef ID_CHAR
900 INSmode(NO); /* We may use real characters ... */
901 # endif
902 HorMin->cm_proc(-1, col);
903 }
904 } else {
905 # ifdef ID_CHAR
906 if (IN_INSmode && !MI)
907 INSmode(NO);
908 # endif
909 (*(void (*)(int, int))DirectMin->cm_proc)(line, col);
910 }
911 }
912
913
914 /* Figures out how many characters ForTab() would use to move forward
915 * using tabs (if possible).
916 * Note: changes to this routine must be matched by changes in ForTab.
917 * An exception is that any cost for leaving insert mode has been
918 * accounted for by our caller.
919 */
920 private int
921 ForNum(int from, int to)
922 {
923 int tabgoal,
924 pts = phystab;
925 int ntabs = 0;
926
927 if ((to > from+1) && TABS && (pts > 0)) {
928 tabgoal = to + (pts / 2);
929 tabgoal -= (tabgoal % pts);
930 if (tabgoal >= CO)
931 tabgoal -= pts;
932 ntabs = (tabgoal / pts) - (from / pts);
933 /* If tabbing moves past goal, and goal is more cols back
934 * than we would have had to move forward from our original
935 * position, tab is counterproductive. Notice that if our
936 * original motion would have been backwards, tab loses too,
937 * so we need not write abs(to-from).
938 */
939 if (tabgoal > to && tabgoal-to >= to-from)
940 ntabs = 0;
941 if (ntabs != 0)
942 from = tabgoal;
943 }
944 return ntabs + (from>to? from-to : to-from);
945 }
946
947 void
948 i_lines(int top, int bottom, int num)
949 {
950 if (CS) {
951 putpad(targ2(CS, bottom, top), 1);
952 CapCol = CapLine = INFINITY; /* actually: unknown */
953 Placur(top, 0);
954 putmulti(SR, M_SR, num, bottom - top);
955 putpad(targ2(CS, ILI, 0), 1);
956 CapCol = CapLine = INFINITY; /* actually: unknown */
957 } else {
958 Placur(bottom - num + 1, 0);
959 putmulti(DL, M_DL, num, ILI - CapLine);
960 Placur(top, 0);
961 putmulti(AL, M_AL, num, ILI - CapLine);
962 }
963 }
964
965 void
966 d_lines(int top, int bottom, int num)
967 {
968 if (CS) {
969 putpad(targ2(CS, bottom, top), 1);
970 CapCol = CapLine = INFINITY; /* actually: unknown */
971 Placur(bottom, 0);
972 putmulti(SF, M_SF, num, bottom - top);
973 putpad(targ2(CS, ILI, 0), 1);
974 CapCol = CapLine = INFINITY; /* actually: unknown */
975 } else {
976 Placur(top, 0);
977 putmulti(DL, M_DL, num, ILI - top);
978 Placur(bottom + 1 - num, 0);
979 putmulti(AL, M_AL, num, ILI - CapLine);
980 }
981 }
982
983 #endif /* TERMCAP */