From a421926e9143550fe19c05496d9995a930eae219 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 25 Jan 2026 12:02:09 +0000 Subject: [PATCH] Fix C module single-box line handling order to match Elisp Bug: C module and Elisp produced different results for same text. Root cause: Different condition checking order for single-box vs last-line: - Elisp: single-box check comes BEFORE is-last check - C: is_last check came BEFORE is_single_box check (now fixed) When the last line has only one box, Elisp uses single-box calculation while C was using last-line calculation, resulting in different demerits and different final breakpoints. Fix: Moved is_single_box check before is_last check in dp_process_position() to match Elisp's ekp--dp-compute-line-demerits behavior. Co-authored-by: Kinneyzhang <38454496+Kinneyzhang@users.noreply.github.com> --- ekp_c/ekp_kp.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/ekp_c/ekp_kp.c b/ekp_c/ekp_kp.c index 6119be9..88f0883 100644 --- a/ekp_c/ekp_kp.c +++ b/ekp_c/ekp_kp.c @@ -275,7 +275,20 @@ static void dp_process_position( uint8_t fit; double dem; - if (is_last) { + /* Single-box line: use fixed flexibility=1, fitness=decent + * This must come BEFORE is_last check to match Elisp behavior + * where single-box lines use consistent calculation */ + if (is_single_box) { + badness = compute_badness(adjustment, 1); + fit = FITNESS_DECENT; + + int penalty = end_hyphen ? in->hyphen_penalty : 0; + dem = prev_dem + compute_demerits(badness, penalty, + prev_fit, fit, + end_hyphen, prev_hyph, + in->line_penalty, + in->fitness_penalty); + } else if (is_last) { /* Last line: minimal penalty if reasonably filled */ double fill_ratio = (double)ideal / line_width; if (fill_ratio < in->last_line_ratio) { @@ -286,17 +299,6 @@ static void dp_process_position( fit = FITNESS_DECENT; dem = prev_dem + (in->line_penalty + badness) * (in->line_penalty + badness); - } else if (is_single_box) { - /* Single-box line: use fixed flexibility=1, fitness=decent */ - badness = compute_badness(adjustment, 1); - fit = FITNESS_DECENT; - - int penalty = end_hyphen ? in->hyphen_penalty : 0; - dem = prev_dem + compute_demerits(badness, penalty, - prev_fit, fit, - end_hyphen, prev_hyph, - in->line_penalty, - in->fitness_penalty); } else { badness = compute_badness(adjustment, flexibility); fit = compute_fitness(adjustment, flexibility);