Hi Michał!
On Di, 07 Aug 2012, Michał Górny wrote:
> Hello,
>
> I am working on a C project where the coding style enforces using tab
> indentation only. Thus, I would like to enforce that policy via
> modeline without affecting user preferred tabstop.
>
> In order to do that, right now I use:
> // vim:noet:sts=0
>
> Sadly, I don't think it is currently possible to reset shiftwidth to
> the value of tabstop. Thus, a user having 'ts=8:sw=4' in his
> preferences will still be inserting spaces.
>
> Thus, I'd like to request the following feature: option to reset
> the shiftwidth to the value of tabstop. Practically, it could be
> implemented by allowing 'sw=0' (much like 'sts=0'), and using the value
> of 'ts' in that case.
>
> Right now, setting 'sw=0' is prohibited so I don't think this would
> introduce any compatibility problems. Of course, the 'sw=0' will be
> useful only in modelines for newer vim versions.
Actually, I think, although Vim complains, It already sets 'sw' to the
current value of 'ts', if 'sw' is less than 1.
,----[ option.c ]-
| [...]
| if (curbuf->b_p_sw <= 0)
| {
| errmsg = e_positive;
| curbuf->b_p_sw = curbuf->b_p_ts;
| }
| [...]
`----
But anyhow, here is a patch, which should do what you want. Let's see,
what Bram thinks.
regards,
Christian
--
Es gibt keinen größeren Hochmut als den der Fachleute.
-- Thornton Niven Wilder
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
diff --git a/src/edit.c b/src/edit.c
--- a/src/edit.c
+++ b/src/edit.c
@@ -8896,10 +8896,13 @@
colnr_T vcol;
colnr_T want_vcol;
colnr_T start_vcol;
+ long sw;
+
+ sw = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
*inserted_space_p = FALSE;
if (p_sta && in_indent)
- ts = curbuf->b_p_sw;
+ ts = sw;
else
ts = curbuf->b_p_sts;
/* Compute the virtual column where we want to be. Since
@@ -9573,6 +9576,9 @@
int ind;
int i;
int temp;
+ long sw;
+
+ sw = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
Insstart_blank_vcol = get_nolist_virtcol();
@@ -9605,7 +9611,7 @@
AppendToRedobuff((char_u *)"\t");
if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
- temp = (int)curbuf->b_p_sw;
+ temp = (int)sw;
else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
temp = (int)curbuf->b_p_sts;
else /* otherwise use 'tabstop' */
diff --git a/src/ex_getln.c b/src/ex_getln.c
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -2268,10 +2268,13 @@
if (c1 == Ctrl_T)
{
+ long sw;
+ sw = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
+
p = (char_u *)line_ga.ga_data;
p[line_ga.ga_len] = NUL;
indent = get_indent_str(p, 8);
- indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
+ indent += sw - indent % sw;
add_indent:
while (get_indent_str(p, 8) < indent)
{
@@ -2320,10 +2323,13 @@
}
else
{
+ long sw;
+ sw = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
+
p[line_ga.ga_len] = NUL;
indent = get_indent_str(p, 8);
--indent;
- indent -= indent % curbuf->b_p_sw;
+ indent -= indent % sw;
}
while (get_indent_str(p, 8) > indent)
{
diff --git a/src/fold.c b/src/fold.c
--- a/src/fold.c
+++ b/src/fold.c
@@ -3010,6 +3010,10 @@
char_u *s;
buf_T *buf;
linenr_T lnum = flp->lnum + flp->off;
+ long sw;
+
+ sw = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
+
buf = flp->wp->w_buffer;
s = skipwhite(ml_get_buf(buf, lnum, FALSE));
@@ -3025,7 +3029,7 @@
flp->lvl = -1;
}
else
- flp->lvl = get_indent_buf(buf, lnum) / buf->b_p_sw;
+ flp->lvl = get_indent_buf(buf, lnum) / sw;
if (flp->lvl > flp->wp->w_p_fdn)
{
flp->lvl = flp->wp->w_p_fdn;
diff --git a/src/misc1.c b/src/misc1.c
--- a/src/misc1.c
+++ b/src/misc1.c
@@ -1389,9 +1389,12 @@
#ifdef FEAT_SMARTINDENT
if (did_si)
{
+ long sw;
+ sw = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
+
if (p_sr)
- newindent -= newindent % (int)curbuf->b_p_sw;
- newindent += (int)curbuf->b_p_sw;
+ newindent -= newindent % (int)sw;
+ newindent += (int)sw;
}
#endif
/* Copy the indent */
@@ -6465,7 +6468,8 @@
* spaces from a block's opening brace the prevailing indent for that
* block should be
*/
- int ind_level = curbuf->b_p_sw;
+
+ int ind_level = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* spaces from the edge of the line an open brace that's at the end of a
@@ -6512,12 +6516,12 @@
/*
* spaces from the switch() indent a "case xx" label should be located
*/
- int ind_case = curbuf->b_p_sw;
+ int ind_case = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* spaces from the "case xx:" code after a switch() should be located
*/
- int ind_case_code = curbuf->b_p_sw;
+ int ind_case_code = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* lineup break at end of case in switch() with case label
@@ -6528,45 +6532,46 @@
* spaces from the class declaration indent a scope declaration label
* should be located
*/
- int ind_scopedecl = curbuf->b_p_sw;
+ int ind_scopedecl = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* spaces from the scope declaration label code should be located
*/
- int ind_scopedecl_code = curbuf->b_p_sw;
+ int ind_scopedecl_code = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* amount K&R-style parameters should be indented
*/
- int ind_param = curbuf->b_p_sw;
+ int ind_param = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* amount a function type spec should be indented
*/
- int ind_func_type = curbuf->b_p_sw;
+ int ind_func_type = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* amount a cpp base class declaration or constructor initialization
* should be indented
*/
- int ind_cpp_baseclass = curbuf->b_p_sw;
+ int ind_cpp_baseclass = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* additional spaces beyond the prevailing indent a continuation line
* should be located
*/
- int ind_continuation = curbuf->b_p_sw;
+ int ind_continuation = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* spaces from the indent of the line with an unclosed parentheses
*/
- int ind_unclosed = curbuf->b_p_sw * 2;
+ int ind_unclosed = (curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts)
+ * 2;
/*
* spaces from the indent of the line with an unclosed parentheses, which
* itself is also unclosed
*/
- int ind_unclosed2 = curbuf->b_p_sw;
+ int ind_unclosed2 = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
/*
* suppress ignoring spaces from the indent of a line starting with an
@@ -6719,12 +6724,13 @@
if (*options == 's') /* "2s" means two times 'shiftwidth' */
{
if (options == digits)
- n = curbuf->b_p_sw; /* just "s" is one 'shiftwidth' */
+ n = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts; /* just "s" is one 'shiftwidth' */
else
{
- n *= curbuf->b_p_sw;
+ long sw = curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
+ n *= sw;
if (divider)
- n += (curbuf->b_p_sw * fraction + divider / 2) / divider;
+ n += (sw * fraction + divider / 2) / divider;
}
++options;
}
diff --git a/src/ops.c b/src/ops.c
--- a/src/ops.c
+++ b/src/ops.c
@@ -332,7 +332,7 @@
{
int count;
int i, j;
- int p_sw = (int)curbuf->b_p_sw;
+ int p_sw = (int)curbuf->b_p_sw ? curbuf->b_p_sw : curbuf->b_p_ts;
count = get_indent(); /* get current indent */
@@ -388,7 +388,8 @@
int total;
char_u *newp, *oldp;
int oldcol = curwin->w_cursor.col;
- int p_sw = (int)curbuf->b_p_sw;
+ int p_sw = (int)(curbuf->b_p_sw ? curbuf->b_p_sw :
+ curbuf->b_p_ts);
int p_ts = (int)curbuf->b_p_ts;
struct block_def bd;
int incr;
diff --git a/src/option.c b/src/option.c
--- a/src/option.c
+++ b/src/option.c
@@ -8125,7 +8125,7 @@
need_mouse_correct = TRUE;
#endif
- if (curbuf->b_p_sw <= 0)
+ if (curbuf->b_p_sw < 0)
{
errmsg = e_positive;
curbuf->b_p_sw = curbuf->b_p_ts;