/contrib/tcsh/ed.refresh.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1330 lines · 935 code · 97 blank · 298 comment · 234 complexity · a83bf12dc0987e34d6f90bddb3ec40ad MD5 · raw file

  1. /* $Header: /p/tcsh/cvsroot/tcsh/ed.refresh.c,v 3.47 2011/02/27 00:14:51 christos Exp $ */
  2. /*
  3. * ed.refresh.c: Lower level screen refreshing functions
  4. */
  5. /*-
  6. * Copyright (c) 1980, 1991 The Regents of the University of California.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "sh.h"
  34. RCSID("$tcsh: ed.refresh.c,v 3.47 2011/02/27 00:14:51 christos Exp $")
  35. #include "ed.h"
  36. /* #define DEBUG_UPDATE */
  37. /* #define DEBUG_REFRESH */
  38. /* #define DEBUG_LITERAL */
  39. /* refresh.c -- refresh the current set of lines on the screen */
  40. Char *litptr;
  41. static int vcursor_h, vcursor_v;
  42. static int rprompt_h, rprompt_v;
  43. static int MakeLiteral (Char *, int, Char);
  44. static int Draw (Char *, int);
  45. static void Vdraw (Char, int);
  46. static void RefreshPromptpart (Char *);
  47. static void update_line (Char *, Char *, int);
  48. static void str_insert (Char *, int, int, Char *, int);
  49. static void str_delete (Char *, int, int, int);
  50. static void str_cp (Char *, Char *, int);
  51. #ifndef WINNT_NATIVE
  52. static
  53. #else
  54. extern
  55. #endif
  56. void PutPlusOne (Char, int);
  57. static void cpy_pad_spaces (Char *, Char *, int);
  58. #if defined(DEBUG_UPDATE) || defined(DEBUG_REFRESH) || defined(DEBUG_LITERAL)
  59. static void reprintf (char *, ...);
  60. #ifdef DEBUG_UPDATE
  61. static void dprintstr (char *, const Char *, const Char *);
  62. static void
  63. dprintstr(char *str, const Char *f, const Char *t)
  64. {
  65. reprintf("%s:\"", str);
  66. while (f < t) {
  67. if (ASC(*f) & ~ASCII)
  68. reprintf("[%x]", *f++);
  69. else
  70. reprintf("%c", CTL_ESC(ASCII & ASC(*f++)));
  71. }
  72. reprintf("\"\r\n");
  73. }
  74. #endif /* DEBUG_UPDATE */
  75. /* reprintf():
  76. * Print to $DEBUGTTY, so that we can test editing on one pty, and
  77. * print debugging stuff on another. Don't interrupt the shell while
  78. * debugging cause you'll mangle up the file descriptors!
  79. */
  80. static void
  81. reprintf(char *fmt, ...)
  82. {
  83. static int fd = -1;
  84. char *dtty;
  85. if ((dtty = getenv("DEBUGTTY"))) {
  86. int o;
  87. va_list va;
  88. va_start(va, fmt);
  89. if (fd == -1)
  90. fd = xopen(dtty, O_RDWR);
  91. o = SHOUT;
  92. flush();
  93. SHOUT = fd;
  94. xvprintf(fmt, va);
  95. va_end(va);
  96. flush();
  97. SHOUT = o;
  98. }
  99. }
  100. #endif /* DEBUG_UPDATE || DEBUG_REFRESH || DEBUG_LITERAL */
  101. static int litlen = 0, litalloc = 0;
  102. static int MakeLiteral(Char *str, int len, Char addlit)
  103. {
  104. int i, addlitlen = 0;
  105. Char *addlitptr = 0;
  106. if (addlit) {
  107. if ((addlit & LITERAL) != 0) {
  108. addlitptr = litptr + (addlit & ~LITERAL) * LIT_FACTOR;
  109. addlitlen = Strlen(addlitptr);
  110. } else {
  111. addlitptr = &addlit;
  112. addlitlen = 1;
  113. }
  114. for (i = 0; i < litlen; i += LIT_FACTOR)
  115. if (!Strncmp(addlitptr, litptr + i, addlitlen) && !Strncmp(str, litptr + i + addlitlen, len) && litptr[i + addlitlen + len] == 0)
  116. return (i / LIT_FACTOR) | LITERAL;
  117. } else {
  118. addlitlen = 0;
  119. for (i = 0; i < litlen; i += LIT_FACTOR)
  120. if (!Strncmp(str, litptr + i, len) && litptr[i + len] == 0)
  121. return (i / LIT_FACTOR) | LITERAL;
  122. }
  123. if (litlen + addlitlen + len + 1 + (LIT_FACTOR - 1) > litalloc) {
  124. Char *newlitptr;
  125. int add = 256;
  126. while (len + addlitlen + 1 + (LIT_FACTOR - 1) > add)
  127. add *= 2;
  128. newlitptr = xrealloc(litptr, (litalloc + add) * sizeof(Char));
  129. if (!newlitptr)
  130. return '?';
  131. litptr = newlitptr;
  132. litalloc += add;
  133. if (addlitptr && addlitptr != &addlit)
  134. addlitptr = litptr + (addlit & ~LITERAL) * LIT_FACTOR;
  135. }
  136. i = litlen / LIT_FACTOR;
  137. if (i >= LITERAL || i == CHAR_DBWIDTH)
  138. return '?';
  139. if (addlitptr) {
  140. Strncpy(litptr + litlen, addlitptr, addlitlen);
  141. litlen += addlitlen;
  142. }
  143. Strncpy(litptr + litlen, str, len);
  144. litlen += len;
  145. do
  146. litptr[litlen++] = 0;
  147. while (litlen % LIT_FACTOR);
  148. return i | LITERAL;
  149. }
  150. static int
  151. Draw(Char *cp, int nocomb) /* draw char at cp, expand tabs, ctl chars */
  152. {
  153. int w, i, lv, lh;
  154. Char c, attr;
  155. attr = *cp & ~CHAR;
  156. c = *cp & CHAR;
  157. w = NLSClassify(c, nocomb);
  158. switch (w) {
  159. case NLSCLASS_NL:
  160. Vdraw('\0', 0); /* assure end of line */
  161. vcursor_h = 0; /* reset cursor pos */
  162. vcursor_v++;
  163. break;
  164. case NLSCLASS_TAB:
  165. do {
  166. Vdraw(' ', 1);
  167. } while ((vcursor_h & 07) != 0);
  168. break;
  169. case NLSCLASS_CTRL:
  170. Vdraw('^' | attr, 1);
  171. if (c == CTL_ESC('\177')) {
  172. Vdraw('?' | attr, 1);
  173. } else {
  174. #ifdef IS_ASCII
  175. /* uncontrolify it; works only for iso8859-1 like sets */
  176. Vdraw(c | 0100 | attr, 1);
  177. #else
  178. Vdraw(_toebcdic[_toascii[c]|0100] | attr, 1);
  179. #endif
  180. }
  181. break;
  182. case NLSCLASS_ILLEGAL:
  183. Vdraw('\\' | attr, 1);
  184. Vdraw((((c >> 6) & 7) + '0') | attr, 1);
  185. Vdraw((((c >> 3) & 7) + '0') | attr, 1);
  186. Vdraw(((c & 7) + '0') | attr, 1);
  187. break;
  188. case NLSCLASS_ILLEGAL2:
  189. case NLSCLASS_ILLEGAL3:
  190. case NLSCLASS_ILLEGAL4:
  191. Vdraw('\\' | attr, 1);
  192. Vdraw('U' | attr, 1);
  193. Vdraw('+' | attr, 1);
  194. for (i = 8 * NLSCLASS_ILLEGAL_SIZE(w) - 4; i >= 0; i -= 4)
  195. Vdraw("0123456789ABCDEF"[(c >> i) & 15] | attr, 1);
  196. break;
  197. case 0:
  198. lv = vcursor_v;
  199. lh = vcursor_h;
  200. for (;;) {
  201. lh--;
  202. if (lh < 0) {
  203. lv--;
  204. if (lv < 0)
  205. break;
  206. lh = Strlen(Vdisplay[lv]) - 1;
  207. }
  208. if (Vdisplay[lv][lh] != CHAR_DBWIDTH)
  209. break;
  210. }
  211. if (lv < 0) {
  212. Vdraw('\\' | attr, 1);
  213. Vdraw((((c >> 6) & 7) + '0') | attr, 1);
  214. Vdraw((((c >> 3) & 7) + '0') | attr, 1);
  215. Vdraw(((c & 7) + '0') | attr, 1);
  216. break;
  217. }
  218. Vdisplay[lv][lh] = MakeLiteral(cp, 1, Vdisplay[lv][lh]);
  219. break;
  220. default:
  221. Vdraw(*cp, w);
  222. break;
  223. }
  224. return 1;
  225. }
  226. static void
  227. Vdraw(Char c, int width) /* draw char c onto V lines */
  228. {
  229. #ifdef DEBUG_REFRESH
  230. # ifdef SHORT_STRINGS
  231. reprintf("Vdrawing %6.6o '%c' %d\r\n", (unsigned)c, (int)(c & ASCII), width);
  232. # else
  233. reprintf("Vdrawing %3.3o '%c' %d\r\n", (unsigned)c, (int)c, width);
  234. # endif /* SHORT_STRNGS */
  235. #endif /* DEBUG_REFRESH */
  236. /* Hopefully this is what all the terminals do with multi-column characters
  237. that "span line breaks". */
  238. while (vcursor_h + width > TermH)
  239. Vdraw(' ', 1);
  240. Vdisplay[vcursor_v][vcursor_h] = c;
  241. if (width)
  242. vcursor_h++; /* advance to next place */
  243. while (--width > 0)
  244. Vdisplay[vcursor_v][vcursor_h++] = CHAR_DBWIDTH;
  245. if (vcursor_h >= TermH) {
  246. Vdisplay[vcursor_v][TermH] = '\0'; /* assure end of line */
  247. vcursor_h = 0; /* reset it. */
  248. vcursor_v++;
  249. #ifdef DEBUG_REFRESH
  250. if (vcursor_v >= TermV) { /* should NEVER happen. */
  251. reprintf("\r\nVdraw: vcursor_v overflow! Vcursor_v == %d > %d\r\n",
  252. vcursor_v, TermV);
  253. abort();
  254. }
  255. #endif /* DEBUG_REFRESH */
  256. }
  257. }
  258. /*
  259. * RefreshPromptpart()
  260. * draws a prompt element, expanding literals (we know it's ASCIZ)
  261. */
  262. static void
  263. RefreshPromptpart(Char *buf)
  264. {
  265. Char *cp;
  266. int w;
  267. if (buf == NULL)
  268. return;
  269. for (cp = buf; *cp; ) {
  270. if (*cp & LITERAL) {
  271. Char *litstart = cp;
  272. while (*cp & LITERAL)
  273. cp++;
  274. if (*cp) {
  275. w = NLSWidth(*cp & CHAR);
  276. Vdraw(MakeLiteral(litstart, cp + 1 - litstart, 0), w);
  277. cp++;
  278. }
  279. else {
  280. /*
  281. * XXX: This is a bug, we lose the last literal, if it is not
  282. * followed by a normal character, but it is too hard to fix
  283. */
  284. break;
  285. }
  286. }
  287. else
  288. cp += Draw(cp, cp == buf);
  289. }
  290. }
  291. /*
  292. * Refresh()
  293. * draws the new virtual screen image from the current input
  294. * line, then goes line-by-line changing the real image to the new
  295. * virtual image. The routine to re-draw a line can be replaced
  296. * easily in hopes of a smarter one being placed there.
  297. */
  298. #ifndef WINNT_NATIVE
  299. static
  300. #endif
  301. int OldvcV = 0;
  302. void
  303. Refresh(void)
  304. {
  305. int cur_line;
  306. Char *cp;
  307. int cur_h, cur_v = 0, new_vcv;
  308. int rhdiff;
  309. Char oldgetting;
  310. #ifdef DEBUG_REFRESH
  311. reprintf("Prompt = :%s:\r\n", short2str(Prompt));
  312. reprintf("InputBuf = :%s:\r\n", short2str(InputBuf));
  313. #endif /* DEBUG_REFRESH */
  314. oldgetting = GettingInput;
  315. GettingInput = 0; /* avoid re-entrance via SIGWINCH */
  316. /* reset the Vdraw cursor, temporarily draw rprompt to calculate its size */
  317. vcursor_h = 0;
  318. vcursor_v = 0;
  319. RefreshPromptpart(RPrompt);
  320. rprompt_h = vcursor_h;
  321. rprompt_v = vcursor_v;
  322. /* reset the Vdraw cursor, draw prompt */
  323. vcursor_h = 0;
  324. vcursor_v = 0;
  325. RefreshPromptpart(Prompt);
  326. cur_h = -1; /* set flag in case I'm not set */
  327. /* draw the current input buffer */
  328. for (cp = InputBuf; (cp < LastChar); ) {
  329. if (cp >= Cursor && cur_h == -1) {
  330. cur_h = vcursor_h; /* save for later */
  331. cur_v = vcursor_v;
  332. Cursor = cp;
  333. }
  334. cp += Draw(cp, cp == InputBuf);
  335. }
  336. if (cur_h == -1) { /* if I haven't been set yet, I'm at the end */
  337. cur_h = vcursor_h;
  338. cur_v = vcursor_v;
  339. }
  340. rhdiff = TermH - vcursor_h - rprompt_h;
  341. if (rprompt_h != 0 && rprompt_v == 0 && vcursor_v == 0 && rhdiff > 1) {
  342. /*
  343. * have a right-hand side prompt that will fit on
  344. * the end of the first line with at least one
  345. * character gap to the input buffer.
  346. */
  347. while (--rhdiff > 0) /* pad out with spaces */
  348. Vdraw(' ', 1);
  349. RefreshPromptpart(RPrompt);
  350. }
  351. else {
  352. rprompt_h = 0; /* flag "not using rprompt" */
  353. rprompt_v = 0;
  354. }
  355. new_vcv = vcursor_v; /* must be done BEFORE the NUL is written */
  356. Vdraw('\0', 1); /* put NUL on end */
  357. #if defined (DEBUG_REFRESH)
  358. reprintf("TermH=%d, vcur_h=%d, vcur_v=%d, Vdisplay[0]=\r\n:%80.80s:\r\n",
  359. TermH, vcursor_h, vcursor_v, short2str(Vdisplay[0]));
  360. #endif /* DEBUG_REFRESH */
  361. #ifdef DEBUG_UPDATE
  362. reprintf("updating %d lines.\r\n", new_vcv);
  363. #endif /* DEBUG_UPDATE */
  364. for (cur_line = 0; cur_line <= new_vcv; cur_line++) {
  365. /* NOTE THAT update_line MAY CHANGE Display[cur_line] */
  366. update_line(Display[cur_line], Vdisplay[cur_line], cur_line);
  367. #ifdef WINNT_NATIVE
  368. flush();
  369. #endif /* WINNT_NATIVE */
  370. /*
  371. * Copy the new line to be the current one, and pad out with spaces
  372. * to the full width of the terminal so that if we try moving the
  373. * cursor by writing the character that is at the end of the
  374. * screen line, it won't be a NUL or some old leftover stuff.
  375. */
  376. cpy_pad_spaces(Display[cur_line], Vdisplay[cur_line], TermH);
  377. }
  378. #ifdef DEBUG_REFRESH
  379. reprintf("\r\nvcursor_v = %d, OldvcV = %d, cur_line = %d\r\n",
  380. vcursor_v, OldvcV, cur_line);
  381. #endif /* DEBUG_REFRESH */
  382. if (OldvcV > new_vcv) {
  383. for (; cur_line <= OldvcV; cur_line++) {
  384. update_line(Display[cur_line], STRNULL, cur_line);
  385. *Display[cur_line] = '\0';
  386. }
  387. }
  388. OldvcV = new_vcv; /* set for next time */
  389. #ifdef DEBUG_REFRESH
  390. reprintf("\r\nCursorH = %d, CursorV = %d, cur_h = %d, cur_v = %d\r\n",
  391. CursorH, CursorV, cur_h, cur_v);
  392. #endif /* DEBUG_REFRESH */
  393. #ifdef WINNT_NATIVE
  394. flush();
  395. #endif /* WINNT_NATIVE */
  396. MoveToLine(cur_v); /* go to where the cursor is */
  397. MoveToChar(cur_h);
  398. SetAttributes(0); /* Clear all attributes */
  399. flush(); /* send the output... */
  400. GettingInput = oldgetting; /* reset to old value */
  401. }
  402. #ifdef notdef
  403. GotoBottom(void)
  404. { /* used to go to last used screen line */
  405. MoveToLine(OldvcV);
  406. }
  407. #endif
  408. void
  409. PastBottom(void)
  410. { /* used to go to last used screen line */
  411. MoveToLine(OldvcV);
  412. (void) putraw('\r');
  413. (void) putraw('\n');
  414. ClearDisp();
  415. flush();
  416. }
  417. /* insert num characters of s into d (in front of the character) at dat,
  418. maximum length of d is dlen */
  419. static void
  420. str_insert(Char *d, int dat, int dlen, Char *s, int num)
  421. {
  422. Char *a, *b;
  423. if (num <= 0)
  424. return;
  425. if (num > dlen - dat)
  426. num = dlen - dat;
  427. #ifdef DEBUG_REFRESH
  428. reprintf("str_insert() starting: %d at %d max %d, d == \"%s\"\n",
  429. num, dat, dlen, short2str(d));
  430. reprintf("s == \"%s\"n", short2str(s));
  431. #endif /* DEBUG_REFRESH */
  432. /* open up the space for num chars */
  433. if (num > 0) {
  434. b = d + dlen - 1;
  435. a = b - num;
  436. while (a >= &d[dat])
  437. *b-- = *a--;
  438. d[dlen] = '\0'; /* just in case */
  439. }
  440. #ifdef DEBUG_REFRESH
  441. reprintf("str_insert() after insert: %d at %d max %d, d == \"%s\"\n",
  442. num, dat, dlen, short2str(d));
  443. reprintf("s == \"%s\"n", short2str(s));
  444. #endif /* DEBUG_REFRESH */
  445. /* copy the characters */
  446. for (a = d + dat; (a < d + dlen) && (num > 0); num--)
  447. *a++ = *s++;
  448. #ifdef DEBUG_REFRESH
  449. reprintf("str_insert() after copy: %d at %d max %d, d == \"%s\"\n",
  450. num, dat, dlen, d, short2str(s));
  451. reprintf("s == \"%s\"n", short2str(s));
  452. #endif /* DEBUG_REFRESH */
  453. }
  454. /* delete num characters d at dat, maximum length of d is dlen */
  455. static void
  456. str_delete(Char *d, int dat, int dlen, int num)
  457. {
  458. Char *a, *b;
  459. if (num <= 0)
  460. return;
  461. if (dat + num >= dlen) {
  462. d[dat] = '\0';
  463. return;
  464. }
  465. #ifdef DEBUG_REFRESH
  466. reprintf("str_delete() starting: %d at %d max %d, d == \"%s\"\n",
  467. num, dat, dlen, short2str(d));
  468. #endif /* DEBUG_REFRESH */
  469. /* open up the space for num chars */
  470. if (num > 0) {
  471. b = d + dat;
  472. a = b + num;
  473. while (a < &d[dlen])
  474. *b++ = *a++;
  475. d[dlen] = '\0'; /* just in case */
  476. }
  477. #ifdef DEBUG_REFRESH
  478. reprintf("str_delete() after delete: %d at %d max %d, d == \"%s\"\n",
  479. num, dat, dlen, short2str(d));
  480. #endif /* DEBUG_REFRESH */
  481. }
  482. static void
  483. str_cp(Char *a, Char *b, int n)
  484. {
  485. while (n-- && *b)
  486. *a++ = *b++;
  487. }
  488. /* ****************************************************************
  489. update_line() is based on finding the middle difference of each line
  490. on the screen; vis:
  491. /old first difference
  492. /beginning of line | /old last same /old EOL
  493. v v v v
  494. old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  495. new: eddie> Oh, my little buggy says to me, as lurgid as
  496. ^ ^ ^ ^
  497. \beginning of line | \new last same \new end of line
  498. \new first difference
  499. all are character pointers for the sake of speed. Special cases for
  500. no differences, as well as for end of line additions must be handled.
  501. **************************************************************** */
  502. /* Minimum at which doing an insert it "worth it". This should be about
  503. * half the "cost" of going into insert mode, inserting a character, and
  504. * going back out. This should really be calculated from the termcap
  505. * data... For the moment, a good number for ANSI terminals.
  506. */
  507. #define MIN_END_KEEP 4
  508. static void /* could be changed to make it smarter */
  509. update_line(Char *old, Char *new, int cur_line)
  510. {
  511. Char *o, *n, *p, c;
  512. Char *ofd, *ols, *oe, *nfd, *nls, *ne;
  513. Char *osb, *ose, *nsb, *nse;
  514. int fx, sx;
  515. /*
  516. * find first diff (won't be CHAR_DBWIDTH in either line)
  517. */
  518. for (o = old, n = new; *o && (*o == *n); o++, n++)
  519. continue;
  520. ofd = o;
  521. nfd = n;
  522. /*
  523. * Find the end of both old and new
  524. */
  525. o = Strend(o);
  526. /*
  527. * Remove any trailing blanks off of the end, being careful not to
  528. * back up past the beginning.
  529. */
  530. if (!(adrof(STRhighlight) && MarkIsSet)) {
  531. while (ofd < o) {
  532. if (o[-1] != ' ')
  533. break;
  534. o--;
  535. }
  536. }
  537. oe = o;
  538. *oe = (Char) 0;
  539. n = Strend(n);
  540. /* remove blanks from end of new */
  541. if (!(adrof(STRhighlight) && MarkIsSet)) {
  542. while (nfd < n) {
  543. if (n[-1] != ' ')
  544. break;
  545. n--;
  546. }
  547. }
  548. ne = n;
  549. *ne = (Char) 0;
  550. /*
  551. * if no diff, continue to next line of redraw
  552. */
  553. if (*ofd == '\0' && *nfd == '\0') {
  554. #ifdef DEBUG_UPDATE
  555. reprintf("no difference.\r\n");
  556. #endif /* DEBUG_UPDATE */
  557. return;
  558. }
  559. /*
  560. * find last same pointer
  561. */
  562. while ((o > ofd) && (n > nfd) && (*--o == *--n))
  563. continue;
  564. if (*o != *n) {
  565. o++;
  566. n++;
  567. }
  568. while (*o == CHAR_DBWIDTH) {
  569. o++;
  570. n++;
  571. }
  572. ols = o;
  573. nls = n;
  574. /*
  575. * find same begining and same end
  576. */
  577. osb = ols;
  578. nsb = nls;
  579. ose = ols;
  580. nse = nls;
  581. /*
  582. * case 1: insert: scan from nfd to nls looking for *ofd
  583. */
  584. if (*ofd) {
  585. for (c = *ofd, n = nfd; n < nls; n++) {
  586. if (c == *n) {
  587. for (o = ofd, p = n; p < nls && o < ols && *o == *p; o++, p++)
  588. continue;
  589. /*
  590. * if the new match is longer and it's worth keeping, then we
  591. * take it
  592. */
  593. if (((nse - nsb) < (p - n)) && (2 * (p - n) > n - nfd)) {
  594. nsb = n;
  595. nse = p;
  596. osb = ofd;
  597. ose = o;
  598. }
  599. }
  600. }
  601. }
  602. /*
  603. * case 2: delete: scan from ofd to ols looking for *nfd
  604. */
  605. if (*nfd) {
  606. for (c = *nfd, o = ofd; o < ols; o++) {
  607. if (c == *o) {
  608. for (n = nfd, p = o; p < ols && n < nls && *p == *n; p++, n++)
  609. continue;
  610. /*
  611. * if the new match is longer and it's worth keeping, then we
  612. * take it
  613. */
  614. if (((ose - osb) < (p - o)) && (2 * (p - o) > o - ofd)) {
  615. nsb = nfd;
  616. nse = n;
  617. osb = o;
  618. ose = p;
  619. }
  620. }
  621. }
  622. }
  623. #ifdef notdef
  624. /*
  625. * If `last same' is before `same end' re-adjust
  626. */
  627. if (ols < ose)
  628. ols = ose;
  629. if (nls < nse)
  630. nls = nse;
  631. #endif
  632. /*
  633. * Pragmatics I: If old trailing whitespace or not enough characters to
  634. * save to be worth it, then don't save the last same info.
  635. */
  636. if ((oe - ols) < MIN_END_KEEP) {
  637. ols = oe;
  638. nls = ne;
  639. }
  640. /*
  641. * Pragmatics II: if the terminal isn't smart enough, make the data dumber
  642. * so the smart update doesn't try anything fancy
  643. */
  644. /*
  645. * fx is the number of characters we need to insert/delete: in the
  646. * beginning to bring the two same begins together
  647. */
  648. fx = (int) ((nsb - nfd) - (osb - ofd));
  649. /*
  650. * sx is the number of characters we need to insert/delete: in the end to
  651. * bring the two same last parts together
  652. */
  653. sx = (int) ((nls - nse) - (ols - ose));
  654. if (!T_CanIns) {
  655. if (fx > 0) {
  656. osb = ols;
  657. ose = ols;
  658. nsb = nls;
  659. nse = nls;
  660. }
  661. if (sx > 0) {
  662. ols = oe;
  663. nls = ne;
  664. }
  665. if ((ols - ofd) < (nls - nfd)) {
  666. ols = oe;
  667. nls = ne;
  668. }
  669. }
  670. if (!T_CanDel) {
  671. if (fx < 0) {
  672. osb = ols;
  673. ose = ols;
  674. nsb = nls;
  675. nse = nls;
  676. }
  677. if (sx < 0) {
  678. ols = oe;
  679. nls = ne;
  680. }
  681. if ((ols - ofd) > (nls - nfd)) {
  682. ols = oe;
  683. nls = ne;
  684. }
  685. }
  686. /*
  687. * Pragmatics III: make sure the middle shifted pointers are correct if
  688. * they don't point to anything (we may have moved ols or nls).
  689. */
  690. /* if the change isn't worth it, don't bother */
  691. /* was: if (osb == ose) */
  692. if ((ose - osb) < MIN_END_KEEP) {
  693. osb = ols;
  694. ose = ols;
  695. nsb = nls;
  696. nse = nls;
  697. }
  698. /*
  699. * Now that we are done with pragmatics we recompute fx, sx
  700. */
  701. fx = (int) ((nsb - nfd) - (osb - ofd));
  702. sx = (int) ((nls - nse) - (ols - ose));
  703. #ifdef DEBUG_UPDATE
  704. reprintf("\n");
  705. reprintf("ofd %d, osb %d, ose %d, ols %d, oe %d\n",
  706. ofd - old, osb - old, ose - old, ols - old, oe - old);
  707. reprintf("nfd %d, nsb %d, nse %d, nls %d, ne %d\n",
  708. nfd - new, nsb - new, nse - new, nls - new, ne - new);
  709. reprintf("xxx-xxx:\"00000000001111111111222222222233333333334\"\r\n");
  710. reprintf("xxx-xxx:\"01234567890123456789012345678901234567890\"\r\n");
  711. dprintstr("old- oe", old, oe);
  712. dprintstr("new- ne", new, ne);
  713. dprintstr("old-ofd", old, ofd);
  714. dprintstr("new-nfd", new, nfd);
  715. dprintstr("ofd-osb", ofd, osb);
  716. dprintstr("nfd-nsb", nfd, nsb);
  717. dprintstr("osb-ose", osb, ose);
  718. dprintstr("nsb-nse", nsb, nse);
  719. dprintstr("ose-ols", ose, ols);
  720. dprintstr("nse-nls", nse, nls);
  721. dprintstr("ols- oe", ols, oe);
  722. dprintstr("nls- ne", nls, ne);
  723. #endif /* DEBUG_UPDATE */
  724. /*
  725. * CursorV to this line cur_line MUST be in this routine so that if we
  726. * don't have to change the line, we don't move to it. CursorH to first
  727. * diff char
  728. */
  729. MoveToLine(cur_line);
  730. /*
  731. * at this point we have something like this:
  732. *
  733. * /old /ofd /osb /ose /ols /oe
  734. * v.....................v v..................v v........v
  735. * eddie> Oh, my fredded gruntle-buggy is to me, as foo var lurgid as
  736. * eddie> Oh, my fredded quiux buggy is to me, as gruntle-lurgid as
  737. * ^.....................^ ^..................^ ^........^
  738. * \new \nfd \nsb \nse \nls \ne
  739. *
  740. * fx is the difference in length between the the chars between nfd and
  741. * nsb, and the chars between ofd and osb, and is thus the number of
  742. * characters to delete if < 0 (new is shorter than old, as above),
  743. * or insert (new is longer than short).
  744. *
  745. * sx is the same for the second differences.
  746. */
  747. /*
  748. * if we have a net insert on the first difference, AND inserting the net
  749. * amount ((nsb-nfd) - (osb-ofd)) won't push the last useful character
  750. * (which is ne if nls != ne, otherwise is nse) off the edge of the screen
  751. * (TermH - 1) else we do the deletes first so that we keep everything we
  752. * need to.
  753. */
  754. /*
  755. * if the last same is the same like the end, there is no last same part,
  756. * otherwise we want to keep the last same part set p to the last useful
  757. * old character
  758. */
  759. p = (ols != oe) ? oe : ose;
  760. /*
  761. * if (There is a diffence in the beginning) && (we need to insert
  762. * characters) && (the number of characters to insert is less than the term
  763. * width) We need to do an insert! else if (we need to delete characters)
  764. * We need to delete characters! else No insert or delete
  765. */
  766. if ((nsb != nfd) && fx > 0 && ((p - old) + fx < TermH)) {
  767. #ifdef DEBUG_UPDATE
  768. reprintf("first diff insert at %d...\r\n", nfd - new);
  769. #endif /* DEBUG_UPDATE */
  770. /*
  771. * Move to the first char to insert, where the first diff is.
  772. */
  773. MoveToChar(nfd - new);
  774. /*
  775. * Check if we have stuff to keep at end
  776. */
  777. if (nsb != ne) {
  778. #ifdef DEBUG_UPDATE
  779. reprintf("with stuff to keep at end\r\n");
  780. #endif /* DEBUG_UPDATE */
  781. /*
  782. * insert fx chars of new starting at nfd
  783. */
  784. if (fx > 0) {
  785. #ifdef DEBUG_UPDATE
  786. if (!T_CanIns)
  787. reprintf(" ERROR: cannot insert in early first diff\n");
  788. #endif /* DEBUG_UPDATE */
  789. Insert_write(nfd, fx);
  790. str_insert(old, (int) (ofd - old), TermH, nfd, fx);
  791. }
  792. /*
  793. * write (nsb-nfd) - fx chars of new starting at (nfd + fx)
  794. */
  795. so_write(nfd + fx, (nsb - nfd) - fx);
  796. str_cp(ofd + fx, nfd + fx, (int) ((nsb - nfd) - fx));
  797. }
  798. else {
  799. #ifdef DEBUG_UPDATE
  800. reprintf("without anything to save\r\n");
  801. #endif /* DEBUG_UPDATE */
  802. so_write(nfd, (nsb - nfd));
  803. str_cp(ofd, nfd, (int) (nsb - nfd));
  804. /*
  805. * Done
  806. */
  807. return;
  808. }
  809. }
  810. else if (fx < 0) {
  811. #ifdef DEBUG_UPDATE
  812. reprintf("first diff delete at %d...\r\n", ofd - old);
  813. #endif /* DEBUG_UPDATE */
  814. /*
  815. * move to the first char to delete where the first diff is
  816. */
  817. MoveToChar(ofd - old);
  818. /*
  819. * Check if we have stuff to save
  820. */
  821. if (osb != oe) {
  822. #ifdef DEBUG_UPDATE
  823. reprintf("with stuff to save at end\r\n");
  824. #endif /* DEBUG_UPDATE */
  825. /*
  826. * fx is less than zero *always* here but we check for code
  827. * symmetry
  828. */
  829. if (fx < 0) {
  830. #ifdef DEBUG_UPDATE
  831. if (!T_CanDel)
  832. reprintf(" ERROR: cannot delete in first diff\n");
  833. #endif /* DEBUG_UPDATE */
  834. DeleteChars(-fx);
  835. str_delete(old, (int) (ofd - old), TermH, -fx);
  836. }
  837. /*
  838. * write (nsb-nfd) chars of new starting at nfd
  839. */
  840. so_write(nfd, (nsb - nfd));
  841. str_cp(ofd, nfd, (int) (nsb - nfd));
  842. }
  843. else {
  844. #ifdef DEBUG_UPDATE
  845. reprintf("but with nothing left to save\r\n");
  846. #endif /* DEBUG_UPDATE */
  847. /*
  848. * write (nsb-nfd) chars of new starting at nfd
  849. */
  850. so_write(nfd, (nsb - nfd));
  851. #ifdef DEBUG_REFRESH
  852. reprintf("cleareol %d\n", (oe - old) - (ne - new));
  853. #endif /* DEBUG_UPDATE */
  854. #ifndef WINNT_NATIVE
  855. ClearEOL((oe - old) - (ne - new));
  856. #else
  857. /*
  858. * The calculation above does not work too well on NT
  859. */
  860. ClearEOL(TermH - CursorH);
  861. #endif /*WINNT_NATIVE*/
  862. /*
  863. * Done
  864. */
  865. return;
  866. }
  867. }
  868. else
  869. fx = 0;
  870. if (sx < 0) {
  871. #ifdef DEBUG_UPDATE
  872. reprintf("second diff delete at %d...\r\n", (ose - old) + fx);
  873. #endif /* DEBUG_UPDATE */
  874. /*
  875. * Check if we have stuff to delete
  876. */
  877. /*
  878. * fx is the number of characters inserted (+) or deleted (-)
  879. */
  880. MoveToChar((ose - old) + fx);
  881. /*
  882. * Check if we have stuff to save
  883. */
  884. if (ols != oe) {
  885. #ifdef DEBUG_UPDATE
  886. reprintf("with stuff to save at end\r\n");
  887. #endif /* DEBUG_UPDATE */
  888. /*
  889. * Again a duplicate test.
  890. */
  891. if (sx < 0) {
  892. #ifdef DEBUG_UPDATE
  893. if (!T_CanDel)
  894. reprintf(" ERROR: cannot delete in second diff\n");
  895. #endif /* DEBUG_UPDATE */
  896. DeleteChars(-sx);
  897. }
  898. /*
  899. * write (nls-nse) chars of new starting at nse
  900. */
  901. so_write(nse, (nls - nse));
  902. }
  903. else {
  904. int olen = (int) (oe - old + fx);
  905. if (olen > TermH)
  906. olen = TermH;
  907. #ifdef DEBUG_UPDATE
  908. reprintf("but with nothing left to save\r\n");
  909. #endif /* DEBUG_UPDATE */
  910. so_write(nse, (nls - nse));
  911. #ifdef DEBUG_REFRESH
  912. reprintf("cleareol %d\n", olen - (ne - new));
  913. #endif /* DEBUG_UPDATE */
  914. #ifndef WINNT_NATIVE
  915. ClearEOL(olen - (ne - new));
  916. #else
  917. /*
  918. * The calculation above does not work too well on NT
  919. */
  920. ClearEOL(TermH - CursorH);
  921. #endif /*WINNT_NATIVE*/
  922. }
  923. }
  924. /*
  925. * if we have a first insert AND WE HAVEN'T ALREADY DONE IT...
  926. */
  927. if ((nsb != nfd) && (osb - ofd) <= (nsb - nfd) && (fx == 0)) {
  928. #ifdef DEBUG_UPDATE
  929. reprintf("late first diff insert at %d...\r\n", nfd - new);
  930. #endif /* DEBUG_UPDATE */
  931. MoveToChar(nfd - new);
  932. /*
  933. * Check if we have stuff to keep at the end
  934. */
  935. if (nsb != ne) {
  936. #ifdef DEBUG_UPDATE
  937. reprintf("with stuff to keep at end\r\n");
  938. #endif /* DEBUG_UPDATE */
  939. /*
  940. * We have to recalculate fx here because we set it
  941. * to zero above as a flag saying that we hadn't done
  942. * an early first insert.
  943. */
  944. fx = (int) ((nsb - nfd) - (osb - ofd));
  945. if (fx > 0) {
  946. /*
  947. * insert fx chars of new starting at nfd
  948. */
  949. #ifdef DEBUG_UPDATE
  950. if (!T_CanIns)
  951. reprintf(" ERROR: cannot insert in late first diff\n");
  952. #endif /* DEBUG_UPDATE */
  953. Insert_write(nfd, fx);
  954. str_insert(old, (int) (ofd - old), TermH, nfd, fx);
  955. }
  956. /*
  957. * write (nsb-nfd) - fx chars of new starting at (nfd + fx)
  958. */
  959. so_write(nfd + fx, (nsb - nfd) - fx);
  960. str_cp(ofd + fx, nfd + fx, (int) ((nsb - nfd) - fx));
  961. }
  962. else {
  963. #ifdef DEBUG_UPDATE
  964. reprintf("without anything to save\r\n");
  965. #endif /* DEBUG_UPDATE */
  966. so_write(nfd, (nsb - nfd));
  967. str_cp(ofd, nfd, (int) (nsb - nfd));
  968. }
  969. }
  970. /*
  971. * line is now NEW up to nse
  972. */
  973. if (sx >= 0) {
  974. #ifdef DEBUG_UPDATE
  975. reprintf("second diff insert at %d...\r\n", nse - new);
  976. #endif /* DEBUG_UPDATE */
  977. MoveToChar(nse - new);
  978. if (ols != oe) {
  979. #ifdef DEBUG_UPDATE
  980. reprintf("with stuff to keep at end\r\n");
  981. #endif /* DEBUG_UPDATE */
  982. if (sx > 0) {
  983. /* insert sx chars of new starting at nse */
  984. #ifdef DEBUG_UPDATE
  985. if (!T_CanIns)
  986. reprintf(" ERROR: cannot insert in second diff\n");
  987. #endif /* DEBUG_UPDATE */
  988. Insert_write(nse, sx);
  989. }
  990. /*
  991. * write (nls-nse) - sx chars of new starting at (nse + sx)
  992. */
  993. so_write(nse + sx, (nls - nse) - sx);
  994. }
  995. else {
  996. #ifdef DEBUG_UPDATE
  997. reprintf("without anything to save\r\n");
  998. #endif /* DEBUG_UPDATE */
  999. so_write(nse, (nls - nse));
  1000. /*
  1001. * No need to do a clear-to-end here because we were doing
  1002. * a second insert, so we will have over written all of the
  1003. * old string.
  1004. */
  1005. }
  1006. }
  1007. #ifdef DEBUG_UPDATE
  1008. reprintf("done.\r\n");
  1009. #endif /* DEBUG_UPDATE */
  1010. }
  1011. static void
  1012. cpy_pad_spaces(Char *dst, Char *src, int width)
  1013. {
  1014. int i;
  1015. for (i = 0; i < width; i++) {
  1016. if (*src == (Char) 0)
  1017. break;
  1018. *dst++ = *src++;
  1019. }
  1020. while (i < width) {
  1021. *dst++ = ' ';
  1022. i++;
  1023. }
  1024. *dst = (Char) 0;
  1025. }
  1026. void
  1027. RefCursor(void)
  1028. { /* only move to new cursor pos */
  1029. Char *cp;
  1030. int w, h, th, v;
  1031. /* first we must find where the cursor is... */
  1032. h = 0;
  1033. v = 0;
  1034. th = TermH; /* optimize for speed */
  1035. for (cp = Prompt; cp != NULL && *cp; ) { /* do prompt */
  1036. if (*cp & LITERAL) {
  1037. cp++;
  1038. continue;
  1039. }
  1040. w = NLSClassify(*cp & CHAR, cp == Prompt);
  1041. cp++;
  1042. switch(w) {
  1043. case NLSCLASS_NL:
  1044. h = 0;
  1045. v++;
  1046. break;
  1047. case NLSCLASS_TAB:
  1048. while (++h & 07)
  1049. ;
  1050. break;
  1051. case NLSCLASS_CTRL:
  1052. h += 2;
  1053. break;
  1054. case NLSCLASS_ILLEGAL:
  1055. h += 4;
  1056. break;
  1057. case NLSCLASS_ILLEGAL2:
  1058. case NLSCLASS_ILLEGAL3:
  1059. case NLSCLASS_ILLEGAL4:
  1060. h += 3 + 2 * NLSCLASS_ILLEGAL_SIZE(w);
  1061. break;
  1062. default:
  1063. h += w;
  1064. }
  1065. if (h >= th) { /* check, extra long tabs picked up here also */
  1066. h -= th;
  1067. v++;
  1068. }
  1069. }
  1070. for (cp = InputBuf; cp < Cursor;) { /* do input buffer to Cursor */
  1071. w = NLSClassify(*cp & CHAR, cp == InputBuf);
  1072. cp++;
  1073. switch(w) {
  1074. case NLSCLASS_NL:
  1075. h = 0;
  1076. v++;
  1077. break;
  1078. case NLSCLASS_TAB:
  1079. while (++h & 07)
  1080. ;
  1081. break;
  1082. case NLSCLASS_CTRL:
  1083. h += 2;
  1084. break;
  1085. case NLSCLASS_ILLEGAL:
  1086. h += 4;
  1087. break;
  1088. case NLSCLASS_ILLEGAL2:
  1089. case NLSCLASS_ILLEGAL3:
  1090. case NLSCLASS_ILLEGAL4:
  1091. h += 3 + 2 * NLSCLASS_ILLEGAL_SIZE(w);
  1092. break;
  1093. default:
  1094. h += w;
  1095. }
  1096. if (h >= th) { /* check, extra long tabs picked up here also */
  1097. h -= th;
  1098. v++;
  1099. }
  1100. }
  1101. /* now go there */
  1102. MoveToLine(v);
  1103. MoveToChar(h);
  1104. if (adrof(STRhighlight) && MarkIsSet) {
  1105. ClearLines();
  1106. ClearDisp();
  1107. Refresh();
  1108. }
  1109. flush();
  1110. }
  1111. #ifndef WINTT_NATIVE
  1112. static void
  1113. PutPlusOne(Char c, int width)
  1114. {
  1115. while (width > 1 && CursorH + width > TermH)
  1116. PutPlusOne(' ', 1);
  1117. if ((c & LITERAL) != 0) {
  1118. Char *d;
  1119. for (d = litptr + (c & ~LITERAL) * LIT_FACTOR; *d; d++)
  1120. (void) putwraw(*d);
  1121. } else {
  1122. (void) putwraw(c);
  1123. }
  1124. Display[CursorV][CursorH++] = (Char) c;
  1125. while (--width > 0)
  1126. Display[CursorV][CursorH++] = CHAR_DBWIDTH;
  1127. if (CursorH >= TermH) { /* if we must overflow */
  1128. CursorH = 0;
  1129. CursorV++;
  1130. OldvcV++;
  1131. if (T_Margin & MARGIN_AUTO) {
  1132. if (T_Margin & MARGIN_MAGIC) {
  1133. (void) putraw(' ');
  1134. (void) putraw('\b');
  1135. }
  1136. }
  1137. else {
  1138. (void) putraw('\r');
  1139. (void) putraw('\n');
  1140. }
  1141. }
  1142. }
  1143. #endif
  1144. void
  1145. RefPlusOne(int l)
  1146. { /* we added just one char, handle it fast.
  1147. * assumes that screen cursor == real cursor */
  1148. Char *cp, c;
  1149. int w;
  1150. if (Cursor != LastChar) {
  1151. Refresh(); /* too hard to handle */
  1152. return;
  1153. }
  1154. if (rprompt_h != 0 && (TermH - CursorH - rprompt_h < 3)) {
  1155. Refresh(); /* clear out rprompt if less than one char gap*/
  1156. return;
  1157. }
  1158. cp = Cursor - l;
  1159. c = *cp & CHAR;
  1160. w = NLSClassify(c, cp == InputBuf);
  1161. switch(w) {
  1162. case NLSCLASS_CTRL:
  1163. PutPlusOne('^', 1);
  1164. if (c == CTL_ESC('\177')) {
  1165. PutPlusOne('?', 1);
  1166. break;
  1167. }
  1168. #ifdef IS_ASCII
  1169. /* uncontrolify it; works only for iso8859-1 like sets */
  1170. PutPlusOne((c | 0100), 1);
  1171. #else
  1172. PutPlusOne(_toebcdic[_toascii[c]|0100], 1);
  1173. #endif
  1174. break;
  1175. case NLSCLASS_ILLEGAL:
  1176. PutPlusOne('\\', 1);
  1177. PutPlusOne(((c >> 6) & 7) + '0', 1);
  1178. PutPlusOne(((c >> 3) & 7) + '0', 1);
  1179. PutPlusOne((c & 7) + '0', 1);
  1180. break;
  1181. case 1:
  1182. if (adrof(STRhighlight) && MarkIsSet)
  1183. StartHighlight();
  1184. if (l > 1)
  1185. PutPlusOne(MakeLiteral(cp, l, 0), 1);
  1186. else
  1187. PutPlusOne(*cp, 1);
  1188. if (adrof(STRhighlight) && MarkIsSet)
  1189. StopHighlight();
  1190. break;
  1191. default:
  1192. Refresh(); /* too hard to handle */
  1193. return;
  1194. }
  1195. flush();
  1196. }
  1197. /* clear the screen buffers so that new new prompt starts fresh. */
  1198. void
  1199. ClearDisp(void)
  1200. {
  1201. int i;
  1202. CursorV = 0; /* clear the display buffer */
  1203. CursorH = 0;
  1204. for (i = 0; i < TermV; i++)
  1205. (void) memset(Display[i], 0, TermH * sizeof(Display[0][0]));
  1206. OldvcV = 0;
  1207. litlen = 0;
  1208. }
  1209. void
  1210. ClearLines(void)
  1211. { /* Make sure all lines are *really* blank */
  1212. int i;
  1213. if (T_CanCEOL) {
  1214. /*
  1215. * Clear the lines from the bottom up so that if we try moving
  1216. * the cursor down by writing the character that is at the end
  1217. * of the screen line, we won't rewrite a character that shouldn't
  1218. * be there.
  1219. */
  1220. for (i = OldvcV; i >= 0; i--) { /* for each line on the screen */
  1221. MoveToLine(i);
  1222. MoveToChar(0);
  1223. ClearEOL(TermH);
  1224. }
  1225. }
  1226. else {
  1227. MoveToLine(OldvcV); /* go to last line */
  1228. (void) putraw('\r'); /* go to BOL */
  1229. (void) putraw('\n'); /* go to new line */
  1230. }
  1231. }