Skip to content

Commit

Permalink
c-mode: respect user defined tab width in indentation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Daniel Hennigar committed Jan 11, 2025
1 parent fbc84fc commit 5358aeb
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/cmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 5358aeb

Please sign in to comment.