display.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /* display.c -- How to display Info windows.
  2. $Id$
  3. Copyright 1993, 1997, 2003, 2004, 2006, 2007, 2008, 2012, 2013,
  4. 2014, 2015, 2016, 2017 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Originally written by Brian Fox. */
  16. #include "info.h"
  17. #include "display.h"
  18. #include "session.h"
  19. #include "tag.h"
  20. #include "signals.h"
  21. #include "variables.h"
  22. static void free_display (DISPLAY_LINE **display);
  23. static DISPLAY_LINE **make_display (int width, int height);
  24. /* An array of display lines which tell us what is currently visible on
  25. the display. */
  26. DISPLAY_LINE **the_display = NULL;
  27. /* Non-zero means do no output. */
  28. int display_inhibited = 0;
  29. /* Initialize THE_DISPLAY to WIDTH and HEIGHT, with nothing in it. */
  30. void
  31. display_initialize_display (int width, int height)
  32. {
  33. free_display (the_display);
  34. the_display = make_display (width, height);
  35. display_clear_display (the_display);
  36. }
  37. /* Clear all of the lines in DISPLAY making the screen blank. */
  38. void
  39. display_clear_display (DISPLAY_LINE **display)
  40. {
  41. register int i;
  42. signal_block_winch ();
  43. for (i = 0; display[i]; i++)
  44. {
  45. display[i]->text[0] = '\0';
  46. display[i]->textlen = 0;
  47. display[i]->inverse = 0;
  48. }
  49. signal_unblock_winch ();
  50. }
  51. /* Non-zero if we didn't completely redisplay a window. */
  52. int display_was_interrupted_p = 0;
  53. /* Check each window on the screen, and update it if it needs updating. */
  54. void
  55. display_update_display (void)
  56. {
  57. register WINDOW *win;
  58. /* Block window resize signals (SIGWINCH) while accessing the the_display
  59. object, because the signal handler may reallocate it out from under our
  60. feet. */
  61. signal_block_winch ();
  62. display_was_interrupted_p = 0;
  63. for (win = windows; win; win = win->next)
  64. {
  65. /* Only re-display visible windows which need updating. */
  66. if ((win->flags & W_WindowVisible) == 0
  67. || (win->flags & W_UpdateWindow) == 0
  68. || win->height == 0)
  69. continue;
  70. display_update_one_window (win);
  71. if (display_was_interrupted_p)
  72. break;
  73. }
  74. /* Always update the echo area. */
  75. display_update_one_window (the_echo_area);
  76. signal_unblock_winch ();
  77. }
  78. /* Return the screen column of where to write to screen to update line to
  79. match A, given that B contains the current state of the line. *PPOS gets
  80. the offset into the string A to write from. */
  81. static int
  82. find_diff (const char *a, size_t alen, const char *b, size_t blen, int *ppos)
  83. {
  84. mbi_iterator_t itra, itrb;
  85. int i;
  86. int pos = 0;
  87. int first_escape = -1;
  88. int escape_pos = -1;
  89. for (i = 0, mbi_init (itra, a, alen), mbi_init (itrb, b, blen);
  90. mbi_avail (itra) && mbi_avail (itrb);
  91. i += wcwidth (itra.cur.wc), mbi_advance (itra), mbi_advance (itrb))
  92. {
  93. if (mb_cmp (mbi_cur (itra), mbi_cur (itrb)))
  94. break;
  95. if (first_escape == -1 && *mbi_cur_ptr (itra) == '\033')
  96. {
  97. first_escape = i;
  98. escape_pos = pos;
  99. }
  100. pos += mb_len (mbi_cur (itra));
  101. }
  102. if (mbi_avail (itra) || mbi_avail (itrb))
  103. {
  104. if (first_escape != -1)
  105. {
  106. *ppos = escape_pos;
  107. return first_escape;
  108. }
  109. else
  110. {
  111. /* If there was a difference in the line, and there was an escape
  112. character, return the position of the escape character, as it could
  113. start a terminal escape sequence. */
  114. *ppos = pos;
  115. return i;
  116. }
  117. }
  118. /* Otherwise, no redrawing is required. */
  119. return -1;
  120. }
  121. /* Update line PL_NUM of the screen to be PRINTED_LINE, which is PL_BYTES long
  122. and takes up PL_CHARS columns. */
  123. static int
  124. display_update_line (long pl_num, char *printed_line,
  125. long pl_bytes, long pl_chars)
  126. {
  127. DISPLAY_LINE **display = the_display;
  128. DISPLAY_LINE *entry;
  129. entry = display[pl_num];
  130. /* We have the exact line as it should appear on the screen.
  131. Check to see if this line matches the one already appearing
  132. on the screen. */
  133. /* If the window is very small, entry might be NULL. */
  134. if (entry)
  135. {
  136. int i, off;
  137. /* If the screen line is inversed, or if the entry is marked as
  138. invalid, then clear the line from the screen first. */
  139. if (entry->inverse)
  140. {
  141. terminal_goto_xy (0, pl_num);
  142. terminal_clear_to_eol ();
  143. entry->inverse = 0;
  144. entry->text[0] = '\0';
  145. entry->textlen = 0;
  146. }
  147. i = find_diff (printed_line, pl_bytes,
  148. entry->text, strlen (entry->text), &off);
  149. /* If the lines differed at all, we must do some redrawing. */
  150. if (i != -1)
  151. {
  152. /* Move to the proper point on the terminal. */
  153. terminal_goto_xy (i, pl_num);
  154. /* If there is any text to print, print it. */
  155. terminal_put_text (printed_line + off);
  156. /* If the printed text didn't extend all the way to the edge
  157. of the screen, and text was appearing between here and the
  158. edge of the screen, clear from here to the end of the
  159. line. */
  160. if ((pl_chars < screenwidth && pl_chars < entry->textlen)
  161. || entry->inverse)
  162. terminal_clear_to_eol ();
  163. fflush (stdout);
  164. /* Update the display text buffer. */
  165. if (strlen (printed_line) > (unsigned int) screenwidth)
  166. /* printed_line[] can include more than screenwidth
  167. characters, e.g. if multibyte encoding is used or
  168. if we are under -R and there are escape sequences
  169. in it. However, entry->text was allocated (in
  170. display_initialize_display) for screenwidth
  171. bytes only. */
  172. entry->text = xrealloc (entry->text, strlen (printed_line) + 1);
  173. strcpy (entry->text + off, printed_line + off);
  174. entry->textlen = pl_chars;
  175. /* Lines showing node text are not in inverse. Only modelines
  176. have that distinction. */
  177. entry->inverse = 0;
  178. }
  179. }
  180. /* A line has been displayed, and the screen reflects that state.
  181. If there is typeahead pending, then let that typeahead be read
  182. now, instead of continuing with the display. */
  183. if (info_any_buffered_input_p ())
  184. {
  185. display_was_interrupted_p = 1;
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. /* Similar to decide_if_in_match, but used for reference highlighting.
  191. Given an array REFERENCES with regions, starting at *REF_INDEX decide
  192. if we are inside a region at offset OFF. The regions are assumed not
  193. to overlap and to be in order. */
  194. static void
  195. decide_if_in_reference (long off, int *in_ref, REFERENCE **references,
  196. int *ref_index)
  197. {
  198. int i = *ref_index;
  199. int m = *in_ref;
  200. for (; (references[i]); i++)
  201. {
  202. if (references[i]->start > off)
  203. break;
  204. m = 1;
  205. if (references[i]->end > off)
  206. break;
  207. m = 0;
  208. }
  209. *ref_index = i;
  210. *in_ref = m;
  211. }
  212. /* Used when processing a line to be displayed from a node. DEFAULT is the
  213. value when the line has no special styles like underlined references or
  214. highlighted search matches. Otherwise, a line is processed once with
  215. COLLECT as the value, and if it differs to what is on the display already,
  216. it is processed with WRITEOUT and written to the display. */
  217. static int writing_out;
  218. #define DEFAULT 0
  219. #define COLLECT 1
  220. #define WRITEOUT 2 /* Values for writing_out global. */
  221. /* Combine rendition masks that are active, in order of priority,
  222. then check what's currently active on the display, and output
  223. the necessary codes to switch. The list of rendition masks is
  224. the complete information about what the style should now be.
  225. RENDITION3 takes priority over RENDITION2, which in turn takes
  226. priority over RENDITION1. */
  227. static void
  228. wrap_terminal_switch_rendition (struct text_buffer *printed_line,
  229. RENDITION rendition1,
  230. RENDITION rendition2,
  231. RENDITION rendition3)
  232. {
  233. long int desired_rendition = 0;
  234. desired_rendition = rendition1.value;
  235. desired_rendition &= ~rendition2.mask;
  236. desired_rendition |= rendition2.value;
  237. desired_rendition &= ~rendition3.mask;
  238. desired_rendition |= rendition3.value;
  239. if (writing_out == WRITEOUT)
  240. terminal_switch_rendition (desired_rendition);
  241. else
  242. {
  243. /* Guarantee that each byte is non-zero, by having at least one
  244. non-zero bit in it. See ZERO1_MASK symbol in display.c. */
  245. desired_rendition = ~desired_rendition;
  246. /* The text added here is only used internally to see when the
  247. display has changed, and is not output to the terminal. */
  248. text_buffer_add_string (printed_line, "\033", 1);
  249. text_buffer_add_string (printed_line, (char *) &desired_rendition,
  250. sizeof (long));
  251. }
  252. }
  253. /* Set in display_update_node_text if matches or references are to be
  254. distinguished with terminal appearance modes. */
  255. static MATCH_STATE *matches;
  256. static REFERENCE **refs;
  257. static size_t match_index;
  258. static int ref_index;
  259. /* Number of screen columns output so far in a line. */
  260. static int pl_chars;
  261. /* Whether we are currently outputting a highlighted reference. This can be
  262. carried over from one line to another. */
  263. static int ref_highlighted;
  264. static int pl_num; /* Number of printed lines done so far. */
  265. RENDITION ref_rendition = {UNDERLINE_MASK, UNDERLINE_MASK};
  266. RENDITION hl_ref_rendition = {UNDERLINE_MASK, 0};
  267. RENDITION match_rendition = {STANDOUT_MASK, STANDOUT_MASK};
  268. /* Process a line from the node in WIN starting at ITER, and advancing ITER
  269. to the end of the line. What is done with the line depends on the value
  270. of WRITING_OUT.
  271. If the line ends in a newline character, set *DELIM to 1. */
  272. static void
  273. display_process_line (WINDOW *win,
  274. mbi_iterator_t *iter_inout,
  275. struct text_buffer *tb_printed_line,
  276. int *delim)
  277. {
  278. mbi_iterator_t iter;
  279. const char *cur_ptr;
  280. size_t pchars = 0; /* Printed chars */
  281. size_t pbytes = 0; /* Bytes to output. */
  282. char *rep;
  283. int in_match = 0;
  284. int in_ref = 0, in_ref_proper = 0;
  285. RENDITION empty = {0, 0};
  286. int point_in_line;
  287. if (win->point >= win->line_starts[win->pagetop + pl_num]
  288. && win->point < win->line_starts[win->pagetop + pl_num + 1])
  289. point_in_line = 1;
  290. else
  291. point_in_line = 0;
  292. iter = *iter_inout;
  293. while (1)
  294. {
  295. int was_in_ref_proper = in_ref_proper;
  296. int was_in_match = in_match;
  297. if (!mbi_avail (iter))
  298. break;
  299. cur_ptr = mbi_cur_ptr (iter);
  300. if (matches && matches_ready (matches)
  301. && !at_end_of_matches (matches, match_index))
  302. {
  303. int was_in_match = in_match;
  304. decide_if_in_match (cur_ptr - win->node->contents,
  305. &in_match, matches, &match_index);
  306. if (!was_in_match && in_match && writing_out == DEFAULT)
  307. writing_out = COLLECT;
  308. }
  309. if (refs && refs[ref_index])
  310. {
  311. int was_in_ref = in_ref;
  312. decide_if_in_reference (cur_ptr - win->node->contents,
  313. &in_ref, refs, &ref_index);
  314. if (was_in_ref && !in_ref)
  315. {
  316. in_ref_proper = ref_highlighted = 0;
  317. }
  318. else if (!was_in_ref && in_ref)
  319. {
  320. if (writing_out == DEFAULT)
  321. writing_out = COLLECT;
  322. /* Decide if this reference should be highlighted. */
  323. if (point_in_line && win->point < refs[ref_index]->end)
  324. {
  325. /* The reference in is the part of the line after
  326. the cursor, or the reference contains the cursor. */
  327. point_in_line = 0;
  328. ref_highlighted = 1;
  329. }
  330. else if (point_in_line
  331. && (!refs[ref_index + 1]
  332. || refs[ref_index + 1]->start
  333. >= win->line_starts[win->pagetop + pl_num + 1]))
  334. {
  335. /* The reference label is before the cursor in
  336. the current line and none occurs after it in
  337. the current line. */
  338. point_in_line = 0;
  339. ref_highlighted = 1;
  340. }
  341. else if (win->point >= refs[ref_index]->start
  342. && win->point < refs[ref_index]->end)
  343. {
  344. /* The point is in a cross-reference, but not in the
  345. current line. */
  346. ref_highlighted = 1;
  347. }
  348. else if (win->point >= win->line_starts
  349. [win->pagetop + pl_num + 1]
  350. && win->point < win->line_starts
  351. [win->pagetop + pl_num + 2]
  352. && refs[ref_index]->end
  353. >= win->line_starts[win->pagetop + pl_num + 1]
  354. && (!refs[ref_index + 1]
  355. || refs[ref_index + 1]->start
  356. >= win->line_starts[win->pagetop + pl_num + 2]))
  357. {
  358. /* Point is in the next line, not inside this reference,
  359. but this reference continues onto the next line and
  360. no other reference follows it in the line. */
  361. ref_highlighted = 1;
  362. }
  363. }
  364. }
  365. if (in_ref && !in_ref_proper && !strchr (" \t", *cur_ptr))
  366. in_ref_proper = 1;
  367. if (was_in_ref_proper != in_ref_proper || was_in_match != in_match)
  368. {
  369. /* Calculate the new rendition for output characters, and call
  370. the function to switch to it. */
  371. RENDITION ref = {0, 0};
  372. RENDITION match = {0, 0};
  373. if (in_ref_proper)
  374. ref = ref_highlighted && hl_ref_rendition.mask
  375. ? hl_ref_rendition : ref_rendition;
  376. if (in_match)
  377. match = match_rendition;
  378. if (!ref_highlighted)
  379. {
  380. wrap_terminal_switch_rendition (tb_printed_line,
  381. ref, match, empty);
  382. }
  383. else
  384. {
  385. wrap_terminal_switch_rendition (tb_printed_line,
  386. match, ref, empty);
  387. }
  388. }
  389. rep = printed_representation (&iter, delim, pl_chars,
  390. &pchars, &pbytes);
  391. /* If a newline character has been seen, or we have reached the
  392. edge of the display. */
  393. if (*delim || pl_chars + pchars >= win->width)
  394. break;
  395. if (rep)
  396. {
  397. if (writing_out != WRITEOUT)
  398. text_buffer_add_string (tb_printed_line, rep, pbytes);
  399. else
  400. terminal_write_chars (rep, pbytes);
  401. pl_chars += pchars;
  402. }
  403. mbi_advance (iter);
  404. }
  405. if (writing_out != DEFAULT)
  406. wrap_terminal_switch_rendition (tb_printed_line, empty, empty, empty);
  407. *iter_inout = iter;
  408. }
  409. /* Update the part of WIN containing text from a node, i.e. not the blank
  410. part at the end or a modeline.
  411. Print each line in the window into our local buffer, and then
  412. check the contents of that buffer against the display. If they
  413. differ, update the display. Return number of lines printed. */
  414. int
  415. display_update_node_text (WINDOW *win)
  416. {
  417. static struct text_buffer tb_printed_line; /* Buffer for a printed line. */
  418. mbi_iterator_t iter; /* Used to iterate through part of node displayed. */
  419. mbi_iterator_t bol_iter; /* Keep reference to beginning of each line. */
  420. int bol_ref_index = 0, bol_match_index = 0;
  421. int bol_ref_highlighted;
  422. int finish;
  423. matches = 0;
  424. refs = 0;
  425. if (match_rendition.mask)
  426. matches = &win->matches;
  427. if (ref_rendition.mask || hl_ref_rendition.mask)
  428. refs = win->node->references;
  429. pl_num = 0;
  430. ref_highlighted = 0;
  431. writing_out = DEFAULT; /* Global variable, declared above. */
  432. ref_index = match_index = 0;
  433. mbi_init (iter, win->node->contents + win->line_starts[win->pagetop],
  434. win->node->nodelen - win->line_starts[win->pagetop]);
  435. mbi_avail (iter);
  436. while (1)
  437. {
  438. int delim;
  439. mbi_copy (&bol_iter, &iter);
  440. bol_ref_index = ref_index;
  441. bol_match_index = match_index;
  442. bol_ref_highlighted = ref_highlighted;
  443. /* Come back here at end of line when write_out == COLLECT */
  444. start_of_line:
  445. pl_chars = 0;
  446. text_buffer_reset (&tb_printed_line);
  447. delim = 0;
  448. /* Check if we have processed all the lines in the window. */
  449. if (pl_num == win->height)
  450. break;
  451. /* Check if this line of the window is off the screen. This might
  452. happen if the screen was resized very small. */
  453. if (win->first_row + pl_num >= screenheight)
  454. break;
  455. display_process_line (win, &iter, &tb_printed_line, &delim);
  456. /* End of printed line. */
  457. text_buffer_add_char (&tb_printed_line, '\0');
  458. finish = 0;
  459. /* If there are no highlighted regions in a line, we output the line with
  460. display_update_line, which does some optimization of the redisplay.
  461. Otherwise, the entire line is output in this function. */
  462. if (writing_out == DEFAULT)
  463. {
  464. finish = display_update_line (win->first_row + pl_num,
  465. text_buffer_base (&tb_printed_line),
  466. text_buffer_off (&tb_printed_line) - 1,
  467. pl_chars);
  468. }
  469. else if (writing_out == COLLECT)
  470. {
  471. /* Check if the line differs from what is already on the display,
  472. and if so, go back to the start of the line and display it for
  473. real. */
  474. DISPLAY_LINE *entry = the_display[win->first_row + pl_num];
  475. if (strcmp (tb_printed_line.base,
  476. the_display[win->first_row + pl_num]->text))
  477. {
  478. if (tb_printed_line.off > screenwidth)
  479. {
  480. entry->text = xrealloc (entry->text,
  481. tb_printed_line.off + 1);
  482. }
  483. strcpy (entry->text, tb_printed_line.base);
  484. /* Record that the contents of this DISPLAY_LINE isn't
  485. literally what is on the display. */
  486. entry->textlen = 0;
  487. entry->inverse = 1;
  488. mbi_copy (&iter, &bol_iter);
  489. mbi_avail (bol_iter);
  490. ref_index = bol_ref_index;
  491. match_index = bol_match_index;
  492. terminal_goto_xy (0, win->first_row + pl_num);
  493. ref_highlighted = bol_ref_highlighted;
  494. writing_out = WRITEOUT;
  495. goto start_of_line;
  496. }
  497. else
  498. writing_out = DEFAULT;
  499. }
  500. else /* writing_out == WRITEOUT */
  501. {
  502. /* We have just written out this line to the display. */
  503. terminal_clear_to_eol ();
  504. writing_out = DEFAULT;
  505. }
  506. /* Check if a line continuation character should be displayed.
  507. Don't print one on the very last line of the display, as this could
  508. cause it to scroll. */
  509. if (delim)
  510. mbi_advance (iter);
  511. else if (win->first_row + pl_num <= the_screen->height - 2)
  512. {
  513. terminal_goto_xy (win->width - 1, win->first_row + pl_num);
  514. if (!(win->flags & W_NoWrap))
  515. terminal_put_text ("\\");
  516. else
  517. {
  518. terminal_put_text ("$");
  519. /* If this window has chosen not to wrap lines, skip to the
  520. end of the logical line in the buffer, and start a new
  521. line here. */
  522. for (; mbi_avail (iter); mbi_advance (iter))
  523. if (mb_len (mbi_cur (iter)) == 1
  524. && *mbi_cur_ptr (iter) == '\n')
  525. {
  526. mbi_advance (iter);
  527. break;
  528. }
  529. }
  530. fflush (stdout);
  531. }
  532. pl_num++;
  533. if (finish)
  534. break; /* Display was interrupted by typed input. */
  535. if (!mbi_avail (iter))
  536. break;
  537. }
  538. /* Unlike search match highlighting, we always turn reference highlighting
  539. off at the end of each line, so the following isn't needed. */
  540. /* terminal_end_underline (); */
  541. return pl_num;
  542. }
  543. #undef DEFAULT
  544. #undef COLLECT
  545. #undef WRITEOUT /* values for writing_out global */
  546. /* Update one window on the screen. */
  547. void
  548. display_update_one_window (WINDOW *win)
  549. {
  550. size_t line_index = 0;
  551. DISPLAY_LINE **display = the_display;
  552. signal_block_winch ();
  553. /* If display is inhibited, that counts as an interrupted display. */
  554. if (display_inhibited)
  555. {
  556. display_was_interrupted_p = 1;
  557. goto funexit;
  558. }
  559. /* If the window has no height, quit now. Strictly speaking, it
  560. should only be necessary to test if the values are equal to zero, since
  561. window_new_screen_size should ensure that the window height/width never
  562. becomes negative, but since historically this has often been the culprit
  563. for crashes, do our best to be doubly safe. */
  564. if (win->height <= 0 || win->width <= 0)
  565. goto funexit;
  566. /* If the window's first row doesn't appear in the_screen, then it
  567. cannot be displayed. This can happen when the_echo_area is the
  568. window to be displayed, and the screen has shrunk to less than one
  569. line. */
  570. if ((win->first_row < 0) || (win->first_row > the_screen->height))
  571. goto funexit;
  572. /* If this window has a modeline, it might need to be redisplayed. Do
  573. this before the rest of the window to aid in navigation in case the
  574. rest of the window is slow to update (for example, if it has lots of
  575. search matches to be displayed). */
  576. if (!(win->flags & W_InhibitMode))
  577. {
  578. window_make_modeline (win);
  579. line_index = win->first_row + win->height;
  580. /* This display line must both be in inverse, and have the same
  581. contents. */
  582. if ((!display[line_index]->inverse
  583. || (strcmp (display[line_index]->text, win->modeline) != 0))
  584. /* Check screen isn't very small. */
  585. && line_index < the_screen->height)
  586. {
  587. terminal_goto_xy (0, line_index);
  588. terminal_begin_inverse ();
  589. terminal_put_text (win->modeline);
  590. terminal_end_inverse ();
  591. strcpy (display[line_index]->text, win->modeline);
  592. display[line_index]->inverse = 1;
  593. display[line_index]->textlen = strlen (win->modeline);
  594. }
  595. }
  596. if (win->node)
  597. {
  598. if (!win->line_starts)
  599. calculate_line_starts (win);
  600. line_index = display_update_node_text (win);
  601. if (display_was_interrupted_p)
  602. goto funexit;
  603. }
  604. /* We have reached the end of the node or the end of the window. If it
  605. is the end of the node, then clear the lines of the window from here
  606. to the end of the window. */
  607. for (; line_index < win->height; line_index++)
  608. {
  609. DISPLAY_LINE *entry = display[win->first_row + line_index];
  610. /* If this line has text on it, or if we don't know what is on the line,
  611. clear this line. */
  612. if (entry->textlen || entry->inverse)
  613. {
  614. entry->textlen = 0;
  615. entry->text[0] = '\0';
  616. entry->inverse = 0;
  617. terminal_goto_xy (0, win->first_row + line_index);
  618. terminal_clear_to_eol ();
  619. fflush (stdout);
  620. if (info_any_buffered_input_p ())
  621. {
  622. display_was_interrupted_p = 1;
  623. goto funexit;
  624. }
  625. }
  626. }
  627. fflush (stdout);
  628. /* Okay, this window doesn't need updating anymore. */
  629. win->flags &= ~W_UpdateWindow;
  630. funexit:
  631. signal_unblock_winch ();
  632. }
  633. /* Scroll screen lines from START inclusive to END exclusive down
  634. by AMOUNT lines. Negative AMOUNT means move them up. */
  635. static void
  636. display_scroll_region (int start, int end, int amount)
  637. {
  638. int i;
  639. DISPLAY_LINE *temp;
  640. /* Do it on the screen. */
  641. terminal_scroll_region (start, end, amount);
  642. /* Now do it in the display buffer so our contents match the screen. */
  643. if (amount > 0)
  644. {
  645. for (i = end - 1; i >= start + amount; i--)
  646. {
  647. /* Swap rows i and (i - amount). */
  648. temp = the_display[i];
  649. the_display[i] = the_display[i - amount];
  650. the_display[i - amount] = temp;
  651. }
  652. /* Clear vacated lines */
  653. for (i = start; i < start + amount && i < end; i++)
  654. {
  655. the_display[i]->text[0] = '\0';
  656. the_display[i]->textlen = 0;
  657. the_display[i]->inverse = 0;
  658. }
  659. }
  660. else
  661. {
  662. amount *= -1;
  663. for (i = start; i <= end - 1 - amount; i++)
  664. {
  665. /* Swap rows i and (i + amount). */
  666. temp = the_display[i];
  667. the_display[i] = the_display[i + amount];
  668. the_display[i + amount] = temp;
  669. }
  670. /* Clear vacated lines */
  671. for (i = end - 1; i >= end - amount && i >= start; i--)
  672. {
  673. the_display[i]->text[0] = '\0';
  674. the_display[i]->textlen = 0;
  675. the_display[i]->inverse = 0;
  676. }
  677. }
  678. }
  679. /* Scroll the region of the_display starting at START, ending at END, and
  680. moving the lines AMOUNT lines. If AMOUNT is less than zero, the lines
  681. are moved up in the screen, otherwise down. Actually, it is possible
  682. for no scrolling to take place in the case that the terminal doesn't
  683. support it. This doesn't matter to us. */
  684. void
  685. display_scroll_display (int start, int end, int amount)
  686. {
  687. register int i, last;
  688. DISPLAY_LINE *temp;
  689. /* If this terminal cannot do scrolling, give up now. */
  690. if (!terminal_can_scroll && !terminal_can_scroll_region)
  691. return;
  692. /* If there isn't anything displayed on the screen because it is too
  693. small, quit now. */
  694. if (!the_display[0])
  695. return;
  696. /* If there is typeahead pending, then don't actually do any scrolling. */
  697. if (info_any_buffered_input_p ())
  698. return;
  699. /* Use scrolling region if we can because it doesn't affect the area
  700. below the area we want to scroll. */
  701. if (terminal_can_scroll_region)
  702. {
  703. display_scroll_region (start, end, amount);
  704. return;
  705. }
  706. /* Otherwise scroll by deleting and inserting lines. */
  707. if (amount < 0)
  708. start -= amount;
  709. else
  710. end -= amount;
  711. /* Do it on the screen. */
  712. terminal_scroll_terminal (start, end, amount);
  713. /* Now do it in the display buffer so our contents match the screen. */
  714. if (amount > 0)
  715. {
  716. last = end + amount;
  717. /* Shift the lines to scroll right into place. */
  718. for (i = 1; i <= (end - start); i++)
  719. {
  720. temp = the_display[last - i];
  721. the_display[last - i] = the_display[end - i];
  722. the_display[end - i] = temp;
  723. }
  724. /* The lines have been shifted down in the buffer. Clear all of the
  725. lines that were vacated. */
  726. for (i = start; i != (start + amount); i++)
  727. {
  728. the_display[i]->text[0] = '\0';
  729. the_display[i]->textlen = 0;
  730. the_display[i]->inverse = 0;
  731. }
  732. }
  733. else
  734. {
  735. last = start + amount;
  736. for (i = 0; i < (end - start); i++)
  737. {
  738. temp = the_display[last + i];
  739. the_display[last + i] = the_display[start + i];
  740. the_display[start + i] = temp;
  741. }
  742. /* The lines have been shifted up in the buffer. Clear all of the
  743. lines that are left over. */
  744. for (i = end + amount; i != end; i++)
  745. {
  746. the_display[i]->text[0] = '\0';
  747. the_display[i]->textlen = 0;
  748. the_display[i]->inverse = 0;
  749. }
  750. }
  751. }
  752. /* Try to scroll lines in WINDOW. OLD_PAGETOP is the pagetop of WINDOW before
  753. having had its line starts recalculated. OLD_STARTS is the list of line
  754. starts that used to appear in this window. OLD_COUNT is the number of lines
  755. that appear in the OLD_STARTS array. */
  756. void
  757. display_scroll_line_starts (WINDOW *window, int old_pagetop,
  758. long *old_starts, int old_count)
  759. {
  760. register int i, old, new; /* Indices into the line starts arrays. */
  761. int last_new, last_old; /* Index of the last visible line. */
  762. int old_first, new_first; /* Index of the first changed line. */
  763. int unchanged_at_top = 0;
  764. int already_scrolled = 0;
  765. /* Locate the first line which was displayed on the old window. */
  766. old_first = old_pagetop;
  767. new_first = window->pagetop;
  768. /* Find the last line currently visible in this window. */
  769. last_new = window->pagetop + (window->height - 1);
  770. if (last_new > window->line_count)
  771. last_new = window->line_count - 1;
  772. /* Find the last line which used to be currently visible in this window. */
  773. last_old = old_pagetop + (window->height - 1);
  774. if (last_old > old_count)
  775. last_old = old_count - 1;
  776. for (old = old_first, new = new_first;
  777. old < last_old && new < last_new;
  778. old++, new++)
  779. if (old_starts[old] != window->line_starts[new])
  780. break;
  781. else
  782. unchanged_at_top++;
  783. /* Loop through the old lines looking for a match in the new lines. */
  784. for (old = old_first + unchanged_at_top; old < last_old; old++)
  785. {
  786. for (new = new_first; new < last_new; new++)
  787. if (old_starts[old] == window->line_starts[new])
  788. {
  789. /* Find the extent of the matching lines. */
  790. for (i = 0; (old + i) < last_old; i++)
  791. if (old_starts[old + i] != window->line_starts[new + i])
  792. break;
  793. /* Scroll these lines if there are enough of them. */
  794. {
  795. int start, end, amount;
  796. start = (window->first_row
  797. + ((old + already_scrolled) - old_pagetop));
  798. amount = new - (old + already_scrolled);
  799. end = window->first_row + window->height;
  800. /* If we are shifting the block of lines down, then the last
  801. AMOUNT lines will become invisible. Thus, don't bother
  802. scrolling them. */
  803. if (amount > 0)
  804. end -= amount;
  805. if ((end - start) > 0)
  806. {
  807. display_scroll_display (start, end, amount);
  808. /* Some lines have been scrolled. Simulate the scrolling
  809. by offsetting the value of the old index. */
  810. old += i;
  811. already_scrolled += amount;
  812. }
  813. }
  814. }
  815. }
  816. }
  817. /* Move the screen cursor to directly over the current character in WINDOW. */
  818. void
  819. display_cursor_at_point (WINDOW *window)
  820. {
  821. int vpos, hpos;
  822. vpos = window_line_of_point (window) - window->pagetop + window->first_row;
  823. hpos = window_get_cursor_column (window);
  824. terminal_goto_xy (hpos, vpos);
  825. fflush (stdout);
  826. }
  827. /* **************************************************************** */
  828. /* */
  829. /* Functions Static to this File */
  830. /* */
  831. /* **************************************************************** */
  832. /* Make a DISPLAY_LINE ** with width and height. */
  833. static DISPLAY_LINE **
  834. make_display (int width, int height)
  835. {
  836. register int i;
  837. DISPLAY_LINE **display;
  838. display = xmalloc ((1 + height) * sizeof (DISPLAY_LINE *));
  839. for (i = 0; i < height; i++)
  840. {
  841. display[i] = xmalloc (sizeof (DISPLAY_LINE));
  842. display[i]->text = xmalloc (1 + width);
  843. display[i]->textlen = 0;
  844. display[i]->inverse = 0;
  845. }
  846. display[i] = NULL;
  847. return display;
  848. }
  849. /* Free the storage allocated to DISPLAY. */
  850. static void
  851. free_display (DISPLAY_LINE **display)
  852. {
  853. register int i;
  854. register DISPLAY_LINE *display_line;
  855. if (!display)
  856. return;
  857. for (i = 0; (display_line = display[i]); i++)
  858. {
  859. free (display_line->text);
  860. free (display_line);
  861. }
  862. free (display);
  863. }