| 1 | /* |
| 2 | Internal file viewer for the Midnight Commander |
| 3 | Function for plain view |
| 4 | |
| 5 | Copyright (C) 1994-2014 |
| 6 | Free Software Foundation, Inc. |
| 7 | |
| 8 | Written by: |
| 9 | Miguel de Icaza, 1994, 1995, 1998 |
| 10 | Janne Kukonlehto, 1994, 1995 |
| 11 | Jakub Jelinek, 1995 |
| 12 | Joseph M. Hinkle, 1996 |
| 13 | Norbert Warmuth, 1997 |
| 14 | Pavel Machek, 1998 |
| 15 | Roland Illig <roland.illig@gmx.de>, 2004, 2005 |
| 16 | Slava Zanko <slavazanko@google.com>, 2009 |
| 17 | Andrew Borodin <aborodin@vmail.ru>, 2009-2014 |
| 18 | Ilia Maslakov <il.smind@gmail.com>, 2009 |
| 19 | Rewritten almost from scratch by: |
| 20 | Egmont Koblinger <egmont@gmail.com>, 2014 |
| 21 | |
| 22 | This file is part of the Midnight Commander. |
| 23 | |
| 24 | The Midnight Commander is free software: you can redistribute it |
| 25 | and/or modify it under the terms of the GNU General Public License as |
| 26 | published by the Free Software Foundation, either version 3 of the License, |
| 27 | or (at your option) any later version. |
| 28 | |
| 29 | The Midnight Commander is distributed in the hope that it will be useful, |
| 30 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 | GNU General Public License for more details. |
| 33 | |
| 34 | You should have received a copy of the GNU General Public License |
| 35 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 36 | |
| 37 | ------------------------------------------------------------------------------------------------ |
| 38 | |
| 39 | The viewer is implemented along the following design principles: |
| 40 | |
| 41 | Goals: Always display simple scripts, double wide (CJK), combining accents and spacing marks |
| 42 | (often used e.g. in Devanagari) perfectly. Make the arrow keys always work correctly. |
| 43 | |
| 44 | Absolutely non-goal: RTL. |
| 45 | |
| 46 | Terminology: |
| 47 | |
| 48 | - A "paragraph" is the text between two adjacent newline characters. A "line" or "row" is a |
| 49 | visual row on the screen. In wrap mode, the viewer formats a paragraph into one or more lines. |
| 50 | |
| 51 | - The Unicode glossary <http://www.unicode.org/glossary/> doesn't seem to have a notion of "base |
| 52 | character followed by zero or more combining characters". The closest matches are "Combining |
| 53 | Character Sequence" meaning a base character followed by one or more combining characters, or |
| 54 | "Grapheme" which seems to exclude non-printable characters such as newline. In this file, |
| 55 | "combining character sequence" (or any obvious abbreviation thereof) means a base character |
| 56 | followed by zero or more (up to a current limit of 4) combining characters. |
| 57 | |
| 58 | ------------------------------------------------------------------------------------------------ |
| 59 | |
| 60 | The parser-formatter is designed to be stateless across paragraphs. This is so that we can walk |
| 61 | backwards without having to reparse the whole file (although we still need to reparse and |
| 62 | reformat the whole paragraph, but it's a lot better). This principle needs to be changed if we |
| 63 | ever get to address tickets 1849/2977, but then we can still store (for efficiency) the parser |
| 64 | state at the beginning of the paragraph, and safely walk backwards if we don't cross an escape |
| 65 | character. |
| 66 | |
| 67 | The parser-formatter, however, definitely needs to carry a state across lines. Currently this |
| 68 | state contains: |
| 69 | |
| 70 | - The logical column (as if we didn't wrap). This is used for handling TAB characters after a |
| 71 | wordwrap consistently with less. |
| 72 | |
| 73 | - Whether the last nroff character was bold or underlined. This is used for displaying the |
| 74 | ambiguous _\b_ sequence consistently with less. |
| 75 | |
| 76 | - Whether the desired way of displaying a lonely combining accent or spacing mark is to place it |
| 77 | over a dotted circle (we do this at the beginning of the paragraph of after a TAB), or to ignore |
| 78 | the combining char and show replacement char for the spacing mark (we do this if e.g. too many |
| 79 | of these were encountered and hence we don't glue them with their base character). |
| 80 | |
| 81 | - (This state needs to be expanded if e.g. we decide to print verbose replacement characters |
| 82 | (e.g. "<U+0080>") and allow these to wrap around lines.) |
| 83 | |
| 84 | The state also contains the file offset, as it doesn't make sense to ever know the state without |
| 85 | knowing the corresponding offset. |
| 86 | |
| 87 | The state depends on various settings (viewer width, encoding, nroff mode, charwrap or wordwrap |
| 88 | mode (if we'll have that one day) etc.), needs to be recomputed if any of these changes. |
| 89 | |
| 90 | Walking forwards is usually relatively easy both in the file and on the screen. Walking |
| 91 | backwards within a paragraph would only be possible in some special cases and even then it would |
| 92 | be painful, so we always walk back to the beginning of the paragraph and reparse-reformat from |
| 93 | there. |
| 94 | |
| 95 | (Walking back within a line in the file would have at least the following difficulties: handling |
| 96 | the parser state; processing invalid UTF-8; processing invalid nroff (e.g. what is "_\bA\bA"?). |
| 97 | Walking back on the display: we wouldn't know where to display the last line of a paragraph, or |
| 98 | where to display a line if its following line starts with a wide (CJK or Tab) character. Long |
| 99 | story short: just forget this approach.) |
| 100 | |
| 101 | Most important variables: |
| 102 | |
| 103 | - dpy_start: Both in unwrap and wrap modes this points to the beginning of the topmost displayed |
| 104 | paragraph. |
| 105 | |
| 106 | - dpy_text_column: Only in unwrap mode, an additional horizontal scroll. |
| 107 | |
| 108 | - dpy_paragraph_skip_lines: Only in wrap mode, an additional vertical scroll (the number of |
| 109 | lines that are scrolled off at the top from the topmost paragraph). |
| 110 | |
| 111 | - dpy_state_top: Only in wrap mode, the offset and parser-formatter state at the line where |
| 112 | displaying the file begins is cached here. |
| 113 | |
| 114 | - dpy_wrap_dirty: If some parameter has changed that makes it necessary to reparse-redisplay the |
| 115 | topmost paragraph. |
| 116 | |
| 117 | In wrap mode, the three variables "dpy_start", "dpy_paragraph_skip_lines" and "dpy_state_top" |
| 118 | are kept consistent. Think of the first two as the ones describing the position, and the third |
| 119 | as a cached value for better performance so that we don't need to wrap the invisible beginning |
| 120 | of the topmost paragraph over and over again. The third value needs to be recomputed each time a |
| 121 | parameter that influences parsing or displaying the file (e.g. width of screen, encoding, nroff |
| 122 | mode) changes, this is signaled by "dpy_wrap_dirty" to force recomputing "dpy_state_top" (and |
| 123 | clamp "dpy_paragraph_skip_lines" if necessary). |
| 124 | |
| 125 | ------------------------------------------------------------------------------------------------ |
| 126 | |
| 127 | Help integration |
| 128 | |
| 129 | I'm planning to port the help viewer to this codebase. |
| 130 | |
| 131 | Splitting at sections would still happen in the help viewer. It would either copy a section, or |
| 132 | set force_max and a similar force_min to limit displaying to one section only. |
| 133 | |
| 134 | Parsing the help format would go next to the nroff parser. The colors, alternate character set, |
| 135 | and emitting the version number would go to the "state". (The version number would be |
| 136 | implemented by emitting remaining characters of a buffer in the "state" one by one, without |
| 137 | advancing in the file position.) |
| 138 | |
| 139 | The active link would be drawn similarly to the search highlight. Other than that, the viewer |
| 140 | wouldn't care about links (except for their color). help.c would keep track of which one is |
| 141 | highlighted, how to advance to the next/prev on an arrow, how the scroll offset needs to be |
| 142 | adjusted when moving, etc. |
| 143 | |
| 144 | Add wrapping at word boundaries to where wrapping at char boundaries happen now. |
| 145 | */ |
| 146 | |
| 147 | #include <config.h> |
| 148 | |
| 149 | #include "lib/global.h" |
| 150 | #include "lib/tty/tty.h" |
| 151 | #include "lib/skin.h" |
| 152 | #include "lib/util.h" /* is_printable() */ |
| 153 | #ifdef HAVE_CHARSET |
| 154 | #include "lib/charsets.h" |
| 155 | #endif |
| 156 | |
| 157 | #include "src/setup.h" /* option_tab_spacing */ |
| 158 | |
| 159 | #include "internal.h" |
| 160 | |
| 161 | /*** global variables ****************************************************************************/ |
| 162 | |
| 163 | /*** file scope macro definitions ****************************************************************/ |
| 164 | |
| 165 | /* The Unicode standard recommends that lonely combining characters are printed over a dotted |
| 166 | * circle. If the terminal is not UTF-8, this will be replaced by a dot anyway. */ |
| 167 | #define BASE_CHARACTER_FOR_LONELY_COMBINING 0x25CC /* dotted circle */ |
| 168 | #define MAX_COMBINING_CHARS 4 /* both slang and ncurses support exactly 4 */ |
| 169 | |
| 170 | /* I think anything other than space (e.g. arrows) just introduce the visual clutter without |
| 171 | * actually adding value. */ |
| 172 | #define PARTIAL_CJK_AT_LEFT_MARGIN ' ' |
| 173 | #define PARTIAL_CJK_AT_RIGHT_MARGIN ' ' |
| 174 | |
| 175 | /* |
| 176 | * Wrap mode: This is for safety so that jumping to the end of file (which already includes |
| 177 | * scrolling back by a page) and then walking backwards is reasonably fast, even if the file is |
| 178 | * extremely large and consists of maybe full zeros or something like that. If there's no newline |
| 179 | * found within this limit, just start displaying from there and see what happens. We might get |
| 180 | * some displaying parameteres (most importantly the columns) incorrect, but at least will show the |
| 181 | * file without spinning the CPU for ages. When scrolling back to that point, the user might see a |
| 182 | * garbled first line (even starting with an invalid partial UTF-8), but then walking back by yet |
| 183 | * another line should fix it. |
| 184 | * |
| 185 | * Unwrap mode: This is not used, we wouldn't be able to do anything reasonable without walking |
| 186 | * back a whole paragraph (well, view->data_area.height paragraphs actually). |
| 187 | */ |
| 188 | #define MAX_BACKWARDS_WALK_IN_PARAGRAPH (100 * 1000) |
| 189 | |
| 190 | /*** file scope type declarations ****************************************************************/ |
| 191 | |
| 192 | /*** file scope variables ************************************************************************/ |
| 193 | |
| 194 | /*** file scope functions ************************************************************************/ |
| 195 | |
| 196 | /* TODO: These methods shouldn't be necessary, see ticket 3257 */ |
| 197 | |
| 198 | static int |
| 199 | mcview_wcwidth (const mcview_t * view, int c) |
| 200 | { |
| 201 | #ifdef HAVE_CHARSET |
| 202 | if (view->utf8) |
| 203 | { |
| 204 | if (g_unichar_iswide (c)) |
| 205 | return 2; |
| 206 | if (g_unichar_iszerowidth (c)) |
| 207 | return 0; |
| 208 | } |
| 209 | #endif /* HAVE_CHARSET */ |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | static gboolean |
| 214 | mcview_ismark (const mcview_t * view, int c) |
| 215 | { |
| 216 | #ifdef HAVE_CHARSET |
| 217 | if (view->utf8) |
| 218 | return g_unichar_ismark (c); |
| 219 | #endif /* HAVE_CHARSET */ |
| 220 | return FALSE; |
| 221 | } |
| 222 | |
| 223 | /* actually is_non_spacing_mark_or_enclosing_mark */ |
| 224 | static gboolean |
| 225 | mcview_is_non_spacing_mark (const mcview_t * view, int c) |
| 226 | { |
| 227 | #ifdef HAVE_CHARSET |
| 228 | if (view->utf8) |
| 229 | { |
| 230 | GUnicodeType type = g_unichar_type (c); |
| 231 | return type == G_UNICODE_NON_SPACING_MARK || type == G_UNICODE_ENCLOSING_MARK; |
| 232 | } |
| 233 | #endif /* HAVE_CHARSET */ |
| 234 | return FALSE; |
| 235 | } |
| 236 | |
| 237 | #if 0 |
| 238 | static gboolean |
| 239 | mcview_is_spacing_mark (const mcview_t * view, int c) |
| 240 | { |
| 241 | #ifdef HAVE_CHARSET |
| 242 | if (view->utf8) |
| 243 | { |
| 244 | return g_unichar_type (c) == G_UNICODE_SPACING_MARK; |
| 245 | } |
| 246 | #endif /* HAVE_CHARSET */ |
| 247 | return FALSE; |
| 248 | } |
| 249 | #endif /* 0 */ |
| 250 | |
| 251 | static gboolean |
| 252 | mcview_isprint (const mcview_t * view, int c) |
| 253 | { |
| 254 | #ifdef HAVE_CHARSET |
| 255 | if (!view->utf8) |
| 256 | c = convert_from_8bit_to_utf_c ((unsigned char) c, view->converter); |
| 257 | return g_unichar_isprint (c); |
| 258 | #endif /* HAVE_CHARSET */ |
| 259 | /* TODO this is very-very buggy by design: ticket 3257 comments 0-1 */ |
| 260 | return is_printable (c); |
| 261 | } |
| 262 | |
| 263 | static int |
| 264 | mcview_char_display (const mcview_t * view, int c, char *s) |
| 265 | { |
| 266 | #ifdef HAVE_CHARSET |
| 267 | if (mc_global.utf8_display) |
| 268 | { |
| 269 | if (!view->utf8) |
| 270 | c = convert_from_8bit_to_utf_c ((unsigned char) c, view->converter); |
| 271 | if (!g_unichar_isprint (c)) |
| 272 | c = '.'; |
| 273 | return g_unichar_to_utf8 (c, s); |
| 274 | } |
| 275 | else if (view->utf8) |
| 276 | { |
| 277 | if (g_unichar_iswide (c)) |
| 278 | { |
| 279 | s[0] = s[1] = '.'; |
| 280 | return 2; |
| 281 | } |
| 282 | if (g_unichar_iszerowidth (c)) |
| 283 | return 0; |
| 284 | /* TODO the is_printable check below will be broken for this */ |
| 285 | c = convert_from_utf_to_current_c (c, view->converter); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | /* TODO the is_printable check below will be broken for this */ |
| 290 | c = convert_to_display_c (c); |
| 291 | } |
| 292 | #endif /* HAVE_CHARSET */ |
| 293 | /* TODO this is very-very buggy by design: ticket 3257 comments 0-1 */ |
| 294 | if (!is_printable (c)) |
| 295 | c = '.'; |
| 296 | *s = c; |
| 297 | return 1; |
| 298 | } |
| 299 | |
| 300 | /* --------------------------------------------------------------------------------------------- */ |
| 301 | |
| 302 | /* |
| 303 | * Just for convenience, a common interface in front of mcview_get_utf and mcview_get_byte, so that |
| 304 | * the caller doesn't have to care about utf8 vs 8-bit modes. |
| 305 | * |
| 306 | * Normally: stores c, updates state, returns TRUE. |
| 307 | * At EOF: state is unchanged, c is undefined, returns FALSE. |
| 308 | * |
| 309 | * Also, temporary hack: handle force_max here. |
| 310 | * TODO: move it to lower layers (datasource.c)? |
| 311 | */ |
| 312 | static gboolean |
| 313 | mcview_get_next_char (mcview_t * view, mcview_state_machine_t * state, int *c) |
| 314 | { |
| 315 | gboolean result; |
| 316 | int bytes_consumed; |
| 317 | |
| 318 | /* Pretend EOF if we reached force_max */ |
| 319 | if (view->force_max >= 0 && state->offset >= view->force_max) |
| 320 | { |
| 321 | return FALSE; |
| 322 | } |
| 323 | #ifdef HAVE_CHARSET |
| 324 | if (view->utf8) |
| 325 | { |
| 326 | *c = mcview_get_utf (view, state->offset, &bytes_consumed, &result); |
| 327 | if (!result) |
| 328 | return FALSE; |
| 329 | /* Pretend EOF if we crossed force_max */ |
| 330 | if (view->force_max >= 0 && state->offset + bytes_consumed > view->force_max) |
| 331 | { |
| 332 | return FALSE; |
| 333 | } |
| 334 | state->offset += bytes_consumed; |
| 335 | return TRUE; |
| 336 | } |
| 337 | #endif /* HAVE_CHARSET */ |
| 338 | if (!mcview_get_byte (view, state->offset, c)) |
| 339 | return FALSE; |
| 340 | state->offset++; |
| 341 | return TRUE; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * This function parses the next nroff character and gives it to you along with its desired color, |
| 346 | * so you never have to care about nroff again. |
| 347 | * |
| 348 | * The nroff mode does the backspace trick for every single character (Unicode codepoint). At least |
| 349 | * that's what the GNU groff 1.22 package produces, and that's what less 458 expects. For |
| 350 | * double-wide characters (CJK), still only a single backspace is emitted. For combining accents |
| 351 | * and such, the print-backspace-print step is repeated for the base character and then for each |
| 352 | * accent separately. |
| 353 | * |
| 354 | * So, the right place for this layer is after the bytes are interpreted in UTF-8, but before |
| 355 | * joining a base character with its combining accents. |
| 356 | * |
| 357 | * Normally: stores c and color, updates state, returns TRUE. |
| 358 | * At EOF: state is unchanged, c and color are undefined, returns FALSE. |
| 359 | * |
| 360 | * color can be null if the caller doesn't care. |
| 361 | */ |
| 362 | static gboolean |
| 363 | mcview_get_next_maybe_nroff_char (mcview_t * view, mcview_state_machine_t * state, int *c, |
| 364 | int *color) |
| 365 | { |
| 366 | mcview_state_machine_t state_after_nroff; |
| 367 | int c2, c3; |
| 368 | |
| 369 | if (color != NULL) |
| 370 | *color = VIEW_NORMAL_COLOR; |
| 371 | |
| 372 | if (!view->text_nroff_mode) |
| 373 | return mcview_get_next_char (view, state, c); |
| 374 | |
| 375 | if (!mcview_get_next_char (view, state, c)) |
| 376 | return FALSE; |
| 377 | /* Don't allow nroff formatting around CR, LF, TAB or other special chars */ |
| 378 | if (!mcview_isprint (view, *c)) |
| 379 | return TRUE; |
| 380 | |
| 381 | state_after_nroff = *state; |
| 382 | |
| 383 | if (!mcview_get_next_char (view, &state_after_nroff, &c2)) |
| 384 | return TRUE; |
| 385 | if (c2 != '\b') |
| 386 | return TRUE; |
| 387 | |
| 388 | if (!mcview_get_next_char (view, &state_after_nroff, &c3)) |
| 389 | return TRUE; |
| 390 | if (!mcview_isprint (view, c3)) |
| 391 | return TRUE; |
| 392 | |
| 393 | if (*c == '_' && c3 == '_') |
| 394 | { |
| 395 | *state = state_after_nroff; |
| 396 | if (color != NULL) |
| 397 | *color = |
| 398 | state->nroff_underscore_is_underlined ? VIEW_UNDERLINED_COLOR : VIEW_BOLD_COLOR; |
| 399 | return TRUE; |
| 400 | } |
| 401 | else if (*c == c3) |
| 402 | { |
| 403 | *state = state_after_nroff; |
| 404 | state->nroff_underscore_is_underlined = FALSE; |
| 405 | if (color != NULL) |
| 406 | *color = VIEW_BOLD_COLOR; |
| 407 | return TRUE; |
| 408 | } |
| 409 | else if (*c == '_') |
| 410 | { |
| 411 | *c = c3; |
| 412 | *state = state_after_nroff; |
| 413 | state->nroff_underscore_is_underlined = TRUE; |
| 414 | if (color != NULL) |
| 415 | *color = VIEW_UNDERLINED_COLOR; |
| 416 | return TRUE; |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | return TRUE; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Get one base character, along with its combining or spacing mark characters. |
| 426 | * |
| 427 | * (A spacing mark is a character that extends the base character's width 1 into a combined |
| 428 | * character of width 2, yet these two character cells should not be separated. E.g. Devanagari |
| 429 | * <U+0939><U+094B>.) |
| 430 | * |
| 431 | * This method exists mainly for two reasons. One is to be able to tell if we fit on the current |
| 432 | * line or need to wrap to the next one. The other is that both slang and ncurses seem to require |
| 433 | * that the character and its combining marks are printed in a single call (or is it just a |
| 434 | * limitation of mc's wrapper to them?). |
| 435 | * |
| 436 | * For convenience, this method takes care of converting CR or CR+LF into LF. |
| 437 | * TODO this should probably happen later, when displaying the file? |
| 438 | * |
| 439 | * Normally: stores cs and color, updates state, returns >= 1 (entries in cs). |
| 440 | * At EOF: state is unchanged, cs and color are undefined, returns 0. |
| 441 | * |
| 442 | * @param view ... |
| 443 | * @param state the parser-formatter state machine's state, updated |
| 444 | * @param cs store the characters here |
| 445 | * @param clen the room available in cs (that is, at most clen-1 combining marks are allowed), must |
| 446 | * be at least 2 |
| 447 | * @param color if non-NULL, store the color here, taken from the first codepoint's color |
| 448 | * @return the number of entries placed in cs, or 0 on EOF |
| 449 | */ |
| 450 | static int |
| 451 | mcview_next_combining_char_sequence (mcview_t * view, mcview_state_machine_t * state, int *cs, |
| 452 | int clen, int *color) |
| 453 | { |
| 454 | int i = 1; |
| 455 | mcview_state_machine_t state_after_combining; |
| 456 | |
| 457 | if (!mcview_get_next_maybe_nroff_char (view, state, cs, color)) |
| 458 | return 0; |
| 459 | |
| 460 | /* Process \r and \r\n newlines. */ |
| 461 | if (cs[0] == '\r') |
| 462 | { |
| 463 | int cnext; |
| 464 | mcview_state_machine_t state_after_crlf = *state; |
| 465 | if (mcview_get_next_maybe_nroff_char (view, &state_after_crlf, &cnext, NULL) |
| 466 | && cnext == '\n') |
| 467 | *state = state_after_crlf; |
| 468 | cs[0] = '\n'; |
| 469 | return 1; |
| 470 | } |
| 471 | |
| 472 | /* We don't want combining over non-printable characters. This includes '\n' and '\t' too. */ |
| 473 | if (!mcview_isprint (view, cs[0])) |
| 474 | return 1; |
| 475 | |
| 476 | if (mcview_ismark (view, cs[0])) |
| 477 | { |
| 478 | if (!state->print_lonely_combining) |
| 479 | { |
| 480 | /* First character is combining. Either just return it, ... */ |
| 481 | return 1; |
| 482 | } |
| 483 | else |
| 484 | { |
| 485 | /* or place this (and subsequent combining ones) over a dotted circle. */ |
| 486 | cs[1] = cs[0]; |
| 487 | cs[0] = BASE_CHARACTER_FOR_LONELY_COMBINING; |
| 488 | i = 2; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (mcview_wcwidth (view, cs[0]) == 2) |
| 493 | { |
| 494 | /* Don't allow combining or spacing mark for wide characters, is this okay? */ |
| 495 | return 1; |
| 496 | } |
| 497 | |
| 498 | /* Look for more combining chars. Either at most clen-1 zero-width combining chars, |
| 499 | * or at most 1 spacing mark. Is this logic correct? */ |
| 500 | for (; i < clen; i++) |
| 501 | { |
| 502 | state_after_combining = *state; |
| 503 | if (!mcview_get_next_maybe_nroff_char (view, &state_after_combining, &cs[i], NULL)) |
| 504 | return i; |
| 505 | if (!mcview_ismark (view, cs[i]) || !mcview_isprint (view, cs[i])) |
| 506 | return i; |
| 507 | if (g_unichar_type (cs[i]) == G_UNICODE_SPACING_MARK) |
| 508 | { |
| 509 | /* Only allow as the first combining char. Stop processing in either case. */ |
| 510 | if (i == 1) |
| 511 | { |
| 512 | *state = state_after_combining; |
| 513 | i++; |
| 514 | } |
| 515 | return i; |
| 516 | } |
| 517 | *state = state_after_combining; |
| 518 | } |
| 519 | return i; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Parse, format and possibly display one visual line of text. |
| 524 | * |
| 525 | * Formatting starts at the given "state" (which encodes the file offset and parser and formatter's |
| 526 | * internal state). In unwrap mode, this should point to the beginning of the paragraph with the |
| 527 | * default state, the additional horizontal scrolling is added here. In wrap mode, this should |
| 528 | * point to the beginning of the line, with the proper state at that point. |
| 529 | * |
| 530 | * In wrap mode, if a line ends in a newline, it is consumed, even if it's exactly at the right |
| 531 | * edge. In unwrap mode, the whole remaining line, including the newline is consumed. Displaying |
| 532 | * the next line should start at "state"'s new value, or if we displayed the bottom line then |
| 533 | * state->offset tells the file offset to be shown in the top bar. |
| 534 | * |
| 535 | * If "row" is offscreen, don't actually display the line but still update "state" and return the |
| 536 | * proper value. This is used by mcview_wrap_move_down to advance in the file. |
| 537 | * |
| 538 | * @param view ... |
| 539 | * @param state the parser-formatter state machine's state, updated |
| 540 | * @param row print to this row |
| 541 | * @param paragraph_ended store TRUE if paragraph ended by newline or EOF, FALSE if wraps to next |
| 542 | * line |
| 543 | * @return the number of rows, that is, 0 if we were already at EOF, otherwise 1 |
| 544 | */ |
| 545 | static int |
| 546 | mcview_display_line (mcview_t * view, mcview_state_machine_t * state, int row, |
| 547 | gboolean * paragraph_ended) |
| 548 | { |
| 549 | const screen_dimen left = view->data_area.left; |
| 550 | const screen_dimen top = view->data_area.top; |
| 551 | const screen_dimen width = view->data_area.width; |
| 552 | const screen_dimen height = view->data_area.height; |
| 553 | off_t dpy_text_column = view->text_wrap_mode ? 0 : view->dpy_text_column; |
| 554 | screen_dimen col = 0; |
| 555 | int color; |
| 556 | int cs[1 + MAX_COMBINING_CHARS]; |
| 557 | int n; |
| 558 | char str[(1 + MAX_COMBINING_CHARS) * UTF8_CHAR_LEN + 1]; |
| 559 | int charwidth; |
| 560 | int i, j; |
| 561 | mcview_state_machine_t state_saved; |
| 562 | |
| 563 | if (paragraph_ended != NULL) |
| 564 | *paragraph_ended = TRUE; |
| 565 | |
| 566 | if (!view->text_wrap_mode && col >= dpy_text_column + width) |
| 567 | { |
| 568 | /* Optimization: Fast forward to the end of the line, rather than carefully |
| 569 | * parsing and then not actually displaying it. */ |
| 570 | off_t eol = mcview_eol (view, state->offset, mcview_get_filesize (view)); |
| 571 | int retval = eol > state->offset ? 1 : 0; |
| 572 | mcview_state_machine_init (state, eol); |
| 573 | return retval; |
| 574 | } |
| 575 | |
| 576 | while (1) |
| 577 | { |
| 578 | state_saved = *state; |
| 579 | n = mcview_next_combining_char_sequence (view, state, cs, 1 + MAX_COMBINING_CHARS, &color); |
| 580 | if (n == 0) |
| 581 | return col > 0 ? 1 : 0; |
| 582 | |
| 583 | if (view->search_start <= state->offset && state->offset < view->search_end) |
| 584 | color = SELECTED_COLOR; |
| 585 | |
| 586 | if (cs[0] == '\n') |
| 587 | { |
| 588 | /* New line: reset all formatting state for the next paragraph. */ |
| 589 | mcview_state_machine_init (state, state->offset); |
| 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | if (mcview_is_non_spacing_mark (view, cs[0])) |
| 594 | { |
| 595 | /* Lonely combining character. Probably leftover after too many combining chars. Just ignore. */ |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | /* Nonprintable, or lonely spacing mark */ |
| 600 | if ((!mcview_isprint (view, cs[0]) || mcview_ismark (view, cs[0])) && cs[0] != '\t') |
| 601 | cs[0] = '.'; |
| 602 | |
| 603 | charwidth = 0; |
| 604 | for (i = 0; i < n; i++) |
| 605 | { |
| 606 | charwidth += mcview_wcwidth (view, cs[i]); |
| 607 | } |
| 608 | |
| 609 | /* Adjust the width for TAB. It's handled below along with the normal characters, |
| 610 | * so that it's wrapped consistently with them, and is painted with the proper |
| 611 | * attributes (although currently it can't have a special color). */ |
| 612 | if (cs[0] == '\t') |
| 613 | { |
| 614 | charwidth = option_tab_spacing - state->unwrapped_column % option_tab_spacing; |
| 615 | state->print_lonely_combining = TRUE; |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | state->print_lonely_combining = FALSE; |
| 620 | } |
| 621 | |
| 622 | /* In wrap mode only: We're done with this row if the character sequence wouldn't fit. |
| 623 | * Except if at the first column, because then it wouldn't fit in the next row either. |
| 624 | * In this extreme case let the unwrapped code below do its best to display it. */ |
| 625 | if (view->text_wrap_mode && (off_t) col + charwidth > dpy_text_column + width && col > 0) |
| 626 | { |
| 627 | *state = state_saved; |
| 628 | if (paragraph_ended != NULL) |
| 629 | *paragraph_ended = FALSE; |
| 630 | return 1; |
| 631 | } |
| 632 | |
| 633 | /* Display, unless outside of the viewport. */ |
| 634 | if (row >= 0 && row < (int) height) |
| 635 | { |
| 636 | if ((off_t) col >= dpy_text_column && |
| 637 | (off_t) col + charwidth <= dpy_text_column + width) |
| 638 | { |
| 639 | /* The combining character sequence fits entirely in the viewport. Print it. */ |
| 640 | tty_setcolor (color); |
| 641 | widget_move (view, top + row, left + ((off_t) col - dpy_text_column)); |
| 642 | if (cs[0] == '\t') |
| 643 | { |
| 644 | for (i = 0; i < charwidth; i++) |
| 645 | tty_print_char (' '); |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | j = 0; |
| 650 | for (i = 0; i < n; i++) |
| 651 | { |
| 652 | j += mcview_char_display (view, cs[i], str + j); |
| 653 | } |
| 654 | str[j] = '\0'; |
| 655 | /* This is probably a bug in our tty layer, but tty_print_string |
| 656 | * normalizes the string, whereas tty_printf doesn't. Don't normalize, |
| 657 | * since we handle combining characters ourselves correctly, it's |
| 658 | * better if they are copy-pasted correctly. Ticket 3255. */ |
| 659 | tty_printf ("%s", str); |
| 660 | } |
| 661 | } |
| 662 | else if ((off_t) col < dpy_text_column && (off_t) col + charwidth > dpy_text_column) |
| 663 | { |
| 664 | /* The combining character sequence would cross the left edge of the viewport. |
| 665 | * This cannot happen with wrap mode. Print replacement character(s), |
| 666 | * or spaces with the correct attributes for partial Tabs. */ |
| 667 | tty_setcolor (color); |
| 668 | for (i = dpy_text_column; |
| 669 | i < (off_t) col + charwidth && i < dpy_text_column + width; i++) |
| 670 | { |
| 671 | widget_move (view, top + row, left + (i - dpy_text_column)); |
| 672 | tty_print_anychar (cs[0] == '\t' ? ' ' : PARTIAL_CJK_AT_LEFT_MARGIN); |
| 673 | } |
| 674 | } |
| 675 | else if ((off_t) col < dpy_text_column + width && |
| 676 | (off_t) col + charwidth > dpy_text_column + width) |
| 677 | { |
| 678 | /* The combining character sequence would cross the right edge of the viewport |
| 679 | * and we're not wrapping. Print replacement character(s), |
| 680 | * or spaces with the correct attributes for partial Tabs. */ |
| 681 | tty_setcolor (color); |
| 682 | for (i = col; i < dpy_text_column + width; i++) |
| 683 | { |
| 684 | widget_move (view, top + row, left + (i - dpy_text_column)); |
| 685 | tty_print_anychar (cs[0] == '\t' ? ' ' : PARTIAL_CJK_AT_RIGHT_MARGIN); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | col += charwidth; |
| 691 | state->unwrapped_column += charwidth; |
| 692 | |
| 693 | if (!view->text_wrap_mode && col >= dpy_text_column + width) |
| 694 | { |
| 695 | /* Optimization: Fast forward to the end of the line, rather than carefully |
| 696 | * parsing and then not actually displaying it. */ |
| 697 | off_t eol = mcview_eol (view, state->offset, mcview_get_filesize (view)); |
| 698 | mcview_state_machine_init (state, eol); |
| 699 | return 1; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Parse, format and possibly display one paragraph (perhaps not from the beginning). |
| 706 | * |
| 707 | * Formatting starts at the given "state" (which encodes the file offset and parser and formatter's |
| 708 | * internal state). In unwrap mode, this should point to the beginning of the paragraph with the |
| 709 | * default state, the additional horizontal scrolling is added here. In wrap mode, this may point |
| 710 | * to the beginning of the line within a paragraph (to display the partial paragraph at the top), |
| 711 | * with the proper state at that point. |
| 712 | * |
| 713 | * Displaying the next paragraph should start at "state"'s new value, or if we displayed the bottom |
| 714 | * line then state->offset tells the file offset to be shown in the top bar. |
| 715 | * |
| 716 | * If "row" is negative, don't display the first abs(row) lines and display the rest from the top. |
| 717 | * This was a nice idea but it's now unused :) |
| 718 | * |
| 719 | * If "row" is too large, don't display the paragraph at all but still return the number of lines. |
| 720 | * This is used when moving upwards. |
| 721 | * |
| 722 | * @param view ... |
| 723 | * @param state the parser-formatter state machine's state, updated |
| 724 | * @param row print starting at this row |
| 725 | * @return the number of rows the paragraphs is wrapped to, that is, 0 if we were already at EOF, |
| 726 | * otherwise 1 in unwrap mode, >= 1 in wrap mode. We stop when reaching the bottom of the |
| 727 | * viewport, it's not counted how many more lines the paragraph would occupy |
| 728 | */ |
| 729 | static int |
| 730 | mcview_display_paragraph (mcview_t * view, mcview_state_machine_t * state, int row) |
| 731 | { |
| 732 | const screen_dimen height = view->data_area.height; |
| 733 | int lines = 0; |
| 734 | gboolean paragraph_ended; |
| 735 | |
| 736 | while (1) |
| 737 | { |
| 738 | lines += mcview_display_line (view, state, row, ¶graph_ended); |
| 739 | if (paragraph_ended) |
| 740 | return lines; |
| 741 | |
| 742 | if (row < (int) height) |
| 743 | { |
| 744 | row++; |
| 745 | /* stop if bottom of screen reached */ |
| 746 | if (row >= (int) height) |
| 747 | return lines; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Recompute dpy_state_top from dpy_start and dpy_paragraph_skip_lines. Clamp |
| 754 | * dpy_paragraph_skip_lines if necessary. |
| 755 | * |
| 756 | * This method should be called in wrap mode after changing one of the parsing or formatting |
| 757 | * properties (e.g. window width, encoding, nroff), or when switching to wrap mode from unwrap or |
| 758 | * hex. |
| 759 | * |
| 760 | * If we stayed within the same paragraph then try to keep the vertical offset within that |
| 761 | * paragraph as well. It might happen though that the paragraph became shorter than our desired |
| 762 | * vertical position, in that case move to its last row. |
| 763 | */ |
| 764 | static void |
| 765 | mcview_wrap_fixup (mcview_t * view) |
| 766 | { |
| 767 | mcview_state_machine_t state_prev; |
| 768 | gboolean paragraph_ended; |
| 769 | int lines = view->dpy_paragraph_skip_lines; |
| 770 | |
| 771 | if (!view->dpy_wrap_dirty) |
| 772 | return; |
| 773 | view->dpy_wrap_dirty = FALSE; |
| 774 | |
| 775 | view->dpy_paragraph_skip_lines = 0; |
| 776 | mcview_state_machine_init (&view->dpy_state_top, view->dpy_start); |
| 777 | |
| 778 | while (lines--) |
| 779 | { |
| 780 | state_prev = view->dpy_state_top; |
| 781 | if (mcview_display_line (view, &view->dpy_state_top, -1, ¶graph_ended) == 0) |
| 782 | break; |
| 783 | if (paragraph_ended) |
| 784 | { |
| 785 | view->dpy_state_top = state_prev; |
| 786 | break; |
| 787 | } |
| 788 | view->dpy_paragraph_skip_lines++; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | /* --------------------------------------------------------------------------------------------- */ |
| 793 | /*** public functions ****************************************************************************/ |
| 794 | /* --------------------------------------------------------------------------------------------- */ |
| 795 | |
| 796 | /* |
| 797 | * In both wrap and unwrap modes, dpy_start points to the beginning of the paragraph. |
| 798 | * |
| 799 | * In unwrap mode, start displaying from this position, probably applying an additional horizontal |
| 800 | * scroll. |
| 801 | * |
| 802 | * In wrap mode, an additional dpy_paragraph_skip_lines lines are skipped from the top of this |
| 803 | * paragraph. dpy_state_top contains the position and parser-formatter state corresponding to the |
| 804 | * top left corner so we can just start rendering from here. Unless dpy_wrap_dirty is set in which |
| 805 | * case dpy_state_top is invalid and we need to recompute first. |
| 806 | */ |
| 807 | void |
| 808 | mcview_display_text (mcview_t * view) |
| 809 | { |
| 810 | const screen_dimen left = view->data_area.left; |
| 811 | const screen_dimen top = view->data_area.top; |
| 812 | const screen_dimen height = view->data_area.height; |
| 813 | int row; |
| 814 | int n; |
| 815 | mcview_state_machine_t state; |
| 816 | gboolean again; |
| 817 | |
| 818 | do |
| 819 | { |
| 820 | again = FALSE; |
| 821 | |
| 822 | mcview_display_clean (view); |
| 823 | mcview_display_ruler (view); |
| 824 | |
| 825 | if (view->text_wrap_mode) |
| 826 | { |
| 827 | mcview_wrap_fixup (view); |
| 828 | state = view->dpy_state_top; |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | mcview_state_machine_init (&state, view->dpy_start); |
| 833 | } |
| 834 | row = 0; |
| 835 | while (row < (int) height) |
| 836 | { |
| 837 | n = mcview_display_paragraph (view, &state, row); |
| 838 | if (n == 0) |
| 839 | { |
| 840 | /* In the rare case that displaying didn't start at the beginning |
| 841 | * of the file, yet there are some empty lines at the bottom, |
| 842 | * scroll the file and display again. This happens when e.g. the |
| 843 | * window is made bigger, or the file becomes shorter due to |
| 844 | * charset change or enabling nroff. */ |
| 845 | if ((view->text_wrap_mode ? view->dpy_state_top.offset : view->dpy_start) > 0) |
| 846 | { |
| 847 | mcview_ascii_move_up (view, height - row); |
| 848 | again = TRUE; |
| 849 | } |
| 850 | break; |
| 851 | } |
| 852 | row += n; |
| 853 | } |
| 854 | } |
| 855 | while (again); |
| 856 | |
| 857 | view->dpy_end = state.offset; |
| 858 | view->dpy_state_bottom = state; |
| 859 | |
| 860 | if (mcview_show_eof != NULL && mcview_show_eof[0] != '\0') |
| 861 | { |
| 862 | while (row < (int) height) |
| 863 | { |
| 864 | widget_move (view, top + row, left); |
| 865 | /* TODO: should make it no wider than the viewport */ |
| 866 | tty_print_string (mcview_show_eof); |
| 867 | row++; |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Move down. |
| 874 | * |
| 875 | * It's very simple. Just invisibly format the next "lines" lines, carefully carrying the formatter |
| 876 | * state in wrap mode. But before each step we need to check if we've already hit the end of the |
| 877 | * file, in that case we can no longer move. This is done by walking from dpy_state_bottom. |
| 878 | * |
| 879 | * Note that this relies on mcview_display_text() setting dpy_state_bottom to its correct value |
| 880 | * upon rendering the screen contents. So don't call this function from other functions (e.g. at |
| 881 | * the bottom of mcview_ascii_move_up()) which invalidate this value. |
| 882 | */ |
| 883 | void |
| 884 | mcview_ascii_move_down (mcview_t * view, off_t lines) |
| 885 | { |
| 886 | gboolean paragraph_ended; |
| 887 | |
| 888 | while (lines--) |
| 889 | { |
| 890 | /* See if there's still data below the bottom line, by imaginarily displaying one |
| 891 | * more line. This takes care of reading more data into growbuf, if required. |
| 892 | * If the end position didn't advance, we're at EOF and bail out. */ |
| 893 | if (mcview_display_line (view, &view->dpy_state_bottom, -1, ¶graph_ended) == 0) |
| 894 | break; |
| 895 | |
| 896 | /* Okay, there's enough data. Move by 1 row at the top, too. No need to check for |
| 897 | * EOF, that can't happen. */ |
| 898 | if (!view->text_wrap_mode) |
| 899 | { |
| 900 | view->dpy_start = mcview_eol (view, view->dpy_start, mcview_get_filesize (view)); |
| 901 | view->dpy_paragraph_skip_lines = 0; |
| 902 | view->dpy_wrap_dirty = TRUE; |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | mcview_display_line (view, &view->dpy_state_top, -1, ¶graph_ended); |
| 907 | if (paragraph_ended) |
| 908 | { |
| 909 | view->dpy_start = view->dpy_state_top.offset; |
| 910 | view->dpy_paragraph_skip_lines = 0; |
| 911 | } |
| 912 | else |
| 913 | { |
| 914 | view->dpy_paragraph_skip_lines++; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * Move up. |
| 922 | * |
| 923 | * Unwrap mode: Piece of cake. Wrap mode: If we'd walk back more than the current line offset |
| 924 | * within the paragraph, we need to jump back to the previous paragraph and compute its height to |
| 925 | * see if we start from that paragraph, and repeat this if necessary. Once we're within the desired |
| 926 | * paragraph, we still need to format it from its beginning to know the state. |
| 927 | * |
| 928 | * See the top of this file for comments about MAX_BACKWARDS_WALK_IN_PARAGRAPH. |
| 929 | * |
| 930 | * force_max is a nice protection against the rare extreme case that the file underneath us |
| 931 | * changes, we don't want to endlessly consume a file of maybe full of zeros upon moving upwards. |
| 932 | */ |
| 933 | void |
| 934 | mcview_ascii_move_up (mcview_t * view, off_t lines) |
| 935 | { |
| 936 | int i; |
| 937 | |
| 938 | if (!view->text_wrap_mode) |
| 939 | { |
| 940 | while (lines--) |
| 941 | view->dpy_start = mcview_bol (view, view->dpy_start - 1, 0); |
| 942 | view->dpy_paragraph_skip_lines = 0; |
| 943 | view->dpy_wrap_dirty = TRUE; |
| 944 | } |
| 945 | else |
| 946 | { |
| 947 | while (lines > view->dpy_paragraph_skip_lines) |
| 948 | { |
| 949 | /* We need to go back to the previous paragraph. */ |
| 950 | if (view->dpy_start == 0) |
| 951 | { |
| 952 | /* Oops, we're already in the first paragraph. */ |
| 953 | view->dpy_paragraph_skip_lines = 0; |
| 954 | mcview_state_machine_init (&view->dpy_state_top, 0); |
| 955 | return; |
| 956 | } |
| 957 | lines -= view->dpy_paragraph_skip_lines; |
| 958 | view->force_max = view->dpy_start; |
| 959 | view->dpy_start = |
| 960 | mcview_bol (view, view->dpy_start - 1, |
| 961 | view->dpy_start - MAX_BACKWARDS_WALK_IN_PARAGRAPH); |
| 962 | mcview_state_machine_init (&view->dpy_state_top, view->dpy_start); |
| 963 | /* This is a tricky way of denoting that we're at the end of the paragraph. |
| 964 | * Normally we'd jump to the next paragraph and reset paragraph_skip_lines. But for |
| 965 | * walking backwards this is exactly what we need. */ |
| 966 | view->dpy_paragraph_skip_lines = |
| 967 | mcview_display_paragraph (view, &view->dpy_state_top, view->data_area.height); |
| 968 | view->force_max = -1; |
| 969 | } |
| 970 | |
| 971 | /* Okay, we have have dpy_start pointing to the desired paragraph, and we still need to |
| 972 | * walk back "lines" lines from the current "dpy_paragraph_skip_lines" offset. We can't do |
| 973 | * that, so walk from the beginning of the paragraph. */ |
| 974 | mcview_state_machine_init (&view->dpy_state_top, view->dpy_start); |
| 975 | view->dpy_paragraph_skip_lines -= lines; |
| 976 | for (i = 0; i < view->dpy_paragraph_skip_lines; i++) |
| 977 | mcview_display_line (view, &view->dpy_state_top, -1, NULL); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | /* --------------------------------------------------------------------------------------------- */ |
| 982 | |
| 983 | void |
| 984 | mcview_state_machine_init (mcview_state_machine_t * state, off_t offset) |
| 985 | { |
| 986 | memset (state, 0, sizeof (*state)); |
| 987 | state->offset = offset; |
| 988 | state->print_lonely_combining = TRUE; |
| 989 | } |
| 990 | |
| 991 | /* --------------------------------------------------------------------------------------------- */ |