n-t-roff / heirloom-ex-vi

The Traditional Vi (vi with many enhancements from Gunnar Ritter)
Other
70 stars 16 forks source link

Add "expandtab" option #6

Open flux77 opened 2 years ago

flux77 commented 2 years ago

Would it be possible to add an expandtab option like that in nvi (FreeBSD, NetBSD, OpenBSD) and Vim? This would be useful for those who prefer to use spaces rather than tabs for indentation.

Which function(s) should I edit to insert spaces when I press Tab?

n-t-roff commented 2 years ago

I'll have a look, but essentially no time, unfortunately. So this may take quite a while. Or someone has a pull-request meanwhile.

cwfoo commented 2 years ago

Good news: I have a patch for The Traditional Vi release 050325.

Bad news:

For ex-vi version 050325, apply the patch below like this:

diff -Naur ex-050325-orig/ex.1 ex-050325/ex.1
--- ex-050325-orig/ex.1
+++ ex-050325/ex.1
@@ -1607,6 +1607,10 @@
 If possible the editor always places the error message in a standout mode of the
 terminal (such as inverse video) instead of ringing the bell.
 .TP
+\fBexpandtab\fR, \fBet\fR  default: noet
+Expand tab characters to space characters when inserting, replacing or shifting
+text, autoindenting, indenting with \fB^T\fR, or outdenting with \fB^D\fR.
+.TP
 \fBexrc\fR         default: noexrc
 If set, the current directory is searched for a
 .I .exrc
diff -Naur ex-050325-orig/ex_data.c ex-050325/ex_data.c
--- ex-050325-orig/ex_data.c
+++ ex-050325/ex_data.c
@@ -128,6 +128,7 @@
    { "directory",  "dir",  STRING,     0,  0,  direct, },
    { "edcompatible","ed",  ONOFF,      0,  0,  0, },
    { "errorbells", "eb",   ONOFF,      0,  0,  0, },
+   { "expandtab",  "et",   ONOFF,      0,  0,  0, },
    { "exrc",   "ex",   ONOFF,      0,  0,  0, },
    { "flash",  "fl",   ONOFF,      1,  1,  0, },
    { "hardtabs",   "ht",   NUMERIC,    8,  8,  0, },
diff -Naur ex-050325-orig/ex_subr.c ex-050325/ex_subr.c
--- ex-050325-orig/ex_subr.c
+++ ex-050325/ex_subr.c
@@ -235,10 +235,12 @@
 char *
 genindent(register int indent)
 {
-   register char *cp;
+   register char *cp = genbuf;

-   for (cp = genbuf; indent >= value(TABSTOP); indent -= value(TABSTOP))
-       *cp++ = '\t';
+   if (!value(EXPANDTAB)) {
+       for (; indent >= value(TABSTOP); indent -= value(TABSTOP))
+           *cp++ = '\t';
+   }
    for (; indent > 0; indent--)
        *cp++ = ' ';
    return (cp);
diff -Naur ex-050325-orig/ex_vops2.c ex-050325/ex_vops2.c
--- ex-050325-orig/ex_vops2.c
+++ ex-050325/ex_vops2.c
@@ -1007,7 +1007,11 @@
            }
            if (gcursor > &genbuf[LBSIZE - 2])
                error(catgets(catd, 1, 235, "Line too long"));
-           gappend(c & TRIM);
+           if (value(EXPANDTAB) && c == '\t') {
+               for (int i = 0; i < value(TABSTOP); i++)
+                   gappend(' ');
+           } else
+               gappend(c & TRIM);
            vcsync();
            if (value(SHOWMATCH) && !iglobp)
                if (c == ')' || c == '}')
License for the changes above: same as the license used by Gunnar Ritter. i.e. ``` This code contains changes by Foo Chuan Wei, 2022. All rights reserved. Conditions 1, 2, and 4 and the no-warranty notice below apply to these changes. Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by the University of California, Berkeley and its contributors. 4. Neither the name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ```

@flux77 @n-t-roff

josephholsten commented 1 year ago

@cwfoo any chance you have that in a branch so I can get that into https://github.com/traditional-vi/ex-vi with attribution?

cwfoo commented 1 year ago

@josephholsten No, I do not have that in a Git branch. I have added license information to the patch. I hope that it is sufficient for you.

Are you familiar with ex-vi's code? If so, I'd be interested to know whether or not my patch is reasonable.