From 5358aeb4c39ac7cc21ad5fd6e5365f47d849dfa0 Mon Sep 17 00:00:00 2001 From: Daniel Hennigar Date: Fri, 10 Jan 2025 21:06:26 -0800 Subject: [PATCH] c-mode: respect user defined tab width in indentation Example: 1. set a custom tab width via `set-tab-width` (e.g., 4). 2. open c-mode, type of block of C code, and indent. Expected behavior: One tab character per indentation level (or spaces if no-tab-mode enabled). Actual behavior: Multiple tabs/spaces inserted to reach an indentation of 8 columns. This patch updates `getindent` to respect the user-defined tab width (`curbp->b_tabw`) when calculating indentation levels, instead of always assuming 8 columns. Previously, when the user set `set-tab-width` to 4, `c-mode` would request 8 columns of indentation, resulting in two tab characters being inserted instead of one. This caused incorrect indentation for KNF-compliant code. --- src/cmode.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/cmode.c b/src/cmode.c index 2763d8d..352367e 100644 --- a/src/cmode.c +++ b/src/cmode.c @@ -22,9 +22,6 @@ extern int changemode(int, int, char *); static int cc_strip_trailp = TRUE; /* Delete Trailing space? */ -static int cc_basic_indent = 8; /* Basic Indent multiple */ -static int cc_cont_indent = 4; /* Continued line indent */ -static int cc_colon_indent = -8; /* Label / case indent */ static int getmatch(int, int); static int getindent(const struct line *, int *); @@ -324,17 +321,17 @@ getindent(const struct line *lp, int *curi) * we continue */ if (colonp) { - *curi += cc_colon_indent; - newind -= cc_colon_indent; + *curi += -curbp->b_tabw; + newind -= -curbp->b_tabw; } - *curi -= (cbrace) * cc_basic_indent; - newind += obrace * cc_basic_indent; + *curi -= (cbrace) * curbp->b_tabw; + newind += obrace * curbp->b_tabw; if (nparen < 0) - newind -= cc_cont_indent; + newind -= curbp->b_tabw / 2; else if (nparen > 0) - newind += cc_cont_indent; + newind += curbp->b_tabw / 2; *curi += nicol;