From 2a8fbfe6e2704011aa521898f2e944e41141acf2 Mon Sep 17 00:00:00 2001 From: millert Date: Fri, 22 Apr 2022 15:48:29 +0000 Subject: [PATCH] vi: apply expandtab to the output of a ! command This is consistent with vim's expandtab behavior. From nvi2 (Craig Leres). OK tb@ --- usr.bin/vi/docs/USD.doc/vi.man/vi.1 | 12 ++++++++---- usr.bin/vi/ex/ex_bang.c | 6 +++++- usr.bin/vi/ex/ex_shift.c | 20 +++++++++++++++++--- usr.bin/vi/include/ex_extern.h | 3 ++- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/usr.bin/vi/docs/USD.doc/vi.man/vi.1 b/usr.bin/vi/docs/USD.doc/vi.man/vi.1 index 04430f14adf..2d6728bb86b 100644 --- a/usr.bin/vi/docs/USD.doc/vi.man/vi.1 +++ b/usr.bin/vi/docs/USD.doc/vi.man/vi.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: vi.1,v 1.80 2022/03/31 17:27:28 naddy Exp $ +.\" $OpenBSD: vi.1,v 1.81 2022/04/22 15:48:29 millert Exp $ .\" .\" Copyright (c) 1994 .\" The Regents of the University of California. All rights reserved. @@ -14,7 +14,7 @@ .\" .\" @(#)vi.1 8.51 (Berkeley) 10/10/96 .\" -.Dd $Mdocdate: March 31 2022 $ +.Dd $Mdocdate: April 22 2022 $ .Dt VI 1 .Os .Sh NAME @@ -2356,8 +2356,12 @@ characters to when inserting, replacing or shifting text, autoindenting, indenting with .Aq Ic control-T , -or outdenting with -.Aq Ic control-D . +outdenting with +.Aq Ic control-D , +or +when filtering lines with the +.Cm !\& +command. .It Cm exrc , ex Bq off Read the startup files in the local directory. .It Cm extended Bq off diff --git a/usr.bin/vi/ex/ex_bang.c b/usr.bin/vi/ex/ex_bang.c index b056058ae3c..cdb7bf2ec57 100644 --- a/usr.bin/vi/ex/ex_bang.c +++ b/usr.bin/vi/ex/ex_bang.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ex_bang.c,v 1.11 2017/04/18 01:45:35 deraadt Exp $ */ +/* $OpenBSD: ex_bang.c,v 1.12 2022/04/22 15:48:29 millert Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -171,6 +171,10 @@ ex_bang(SCR *sp, EXCMD *cmdp) if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT)) (void)ex_puts(sp, "!\n"); + /* Apply expandtab to the new text */ + if (O_ISSET(sp, O_EXPANDTAB)) + ex_retab(sp, cmdp); + /* * XXX * The ! commands never return an error, so that autoprint always diff --git a/usr.bin/vi/ex/ex_shift.c b/usr.bin/vi/ex/ex_shift.c index b9ab249c1fb..e7f45c1916c 100644 --- a/usr.bin/vi/ex/ex_shift.c +++ b/usr.bin/vi/ex/ex_shift.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ex_shift.c,v 1.9 2020/04/30 10:40:21 millert Exp $ */ +/* $OpenBSD: ex_shift.c,v 1.10 2022/04/22 15:48:29 millert Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -21,7 +21,7 @@ #include "../common/common.h" -enum which {LEFT, RIGHT}; +enum which {RETAB, LEFT, RIGHT}; static int shift(SCR *, EXCMD *, enum which); /* @@ -47,6 +47,18 @@ ex_shiftr(SCR *sp, EXCMD *cmdp) return (shift(sp, cmdp, RIGHT)); } +/* + * ex_retab -- Expand tabs (if enabled) + * + * + * PUBLIC: int ex_retab(SCR *, EXCMD *); + */ +int +ex_retab(SCR *sp, EXCMD *cmdp) +{ + return (shift(sp, cmdp, RETAB)); +} + /* * shift -- * Ex shift support. @@ -109,7 +121,9 @@ shift(SCR *sp, EXCMD *cmdp, enum which rl) break; /* Calculate the new indent amount. */ - if (rl == RIGHT) + if (rl == RETAB) + newcol = oldcol; + else if (rl == RIGHT) newcol = oldcol + sw; else { newcol = oldcol < sw ? 0 : oldcol - sw; diff --git a/usr.bin/vi/include/ex_extern.h b/usr.bin/vi/include/ex_extern.h index 8aa2b6b8268..d44950a55f1 100644 --- a/usr.bin/vi/include/ex_extern.h +++ b/usr.bin/vi/include/ex_extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ex_extern.h,v 1.16 2016/05/27 09:18:12 martijn Exp $ */ +/* $OpenBSD: ex_extern.h,v 1.17 2022/04/22 15:48:29 millert Exp $ */ int ex(SCR **); int ex_cmd(SCR *); @@ -76,6 +76,7 @@ int ex_set(SCR *, EXCMD *); int ex_shell(SCR *, EXCMD *); int ex_exec_proc(SCR *, EXCMD *, char *, const char *, int); int proc_wait(SCR *, pid_t, const char *, int, int); +int ex_retab(SCR *, EXCMD *); int ex_shiftl(SCR *, EXCMD *); int ex_shiftr(SCR *, EXCMD *); int ex_source(SCR *, EXCMD *); -- 2.20.1