|
| 1 | +/****************************************************************************** |
| 2 | + * MODULE : code_wrap.hpp |
| 3 | + * DESCRIPTION: helpers for safe code wrapping boundaries |
| 4 | + * COPYRIGHT : (C) 2026 The MoganSTEM contributors |
| 5 | + ******************************************************************************* |
| 6 | + * This software falls under the GNU general public license version 3 or later. |
| 7 | + * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE |
| 8 | + * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>. |
| 9 | + ******************************************************************************/ |
| 10 | + |
| 11 | +#ifndef TM_CODE_WRAP_HPP |
| 12 | +#define TM_CODE_WRAP_HPP |
| 13 | + |
| 14 | +#include "basic.hpp" |
| 15 | +#include "string.hpp" |
| 16 | +// Protect TeXmacs internal escape sequences like "<#4E2D>" (CJK, etc.) |
| 17 | +// from being split during automatic line wrapping in code/prog environments. |
| 18 | +// |
| 19 | +// NOTE: |
| 20 | +// - We only protect "<#...>" to avoid affecting normal code like "<tag>". |
| 21 | +// - This is a last-resort safety net: even if the line breaker proposes an |
| 22 | +// invalid split position, we snap it to a valid boundary here. |
| 23 | +static inline int |
| 24 | +tm_atom_end_for_code_wrap (string s, int i) { |
| 25 | + int n= N (s); |
| 26 | + if (i < 0 || i >= n) return i; |
| 27 | + if (s[i] != '<') return i + 1; |
| 28 | + |
| 29 | + if (i + 1 >= n || s[i + 1] != '#') return i + 1; |
| 30 | + |
| 31 | + int j= i + 2; |
| 32 | + while (j < n && s[j] != '>') |
| 33 | + j++; |
| 34 | + if (j < n && s[j] == '>') return j + 1; |
| 35 | + |
| 36 | + return i + 1; |
| 37 | +} |
| 38 | + |
| 39 | +static inline int |
| 40 | +tm_snap_after_boundary_for_code_wrap (string s, int after) { |
| 41 | + int n= N (s); |
| 42 | + if (after <= 0) return 0; |
| 43 | + if (after >= n) return n; |
| 44 | + |
| 45 | + int i = 0; |
| 46 | + int last= 0; |
| 47 | + while (i < n) { |
| 48 | + int j= tm_atom_end_for_code_wrap (s, i); |
| 49 | + if (j > after) break; |
| 50 | + last= j; |
| 51 | + i = j; |
| 52 | + } |
| 53 | + return last; |
| 54 | +} |
| 55 | + |
| 56 | +#endif // TM_CODE_WRAP_HPP |
0 commit comments