9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] [PATCH] Spaceindent mode for acme
@ 2021-08-05 16:59 binarycat
  2021-08-05 17:28 ` Rodrigo G. López
  0 siblings, 1 reply; 3+ messages in thread
From: binarycat @ 2021-08-05 16:59 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 813 bytes --]

I do prefer tabs over spaces, but sometimes you have to use spaces.
For example, zig doesn't even allow the tab charachter in its source
code.

So, I put together a simple patch that adds a command `Spaceindent`.
This command works similar to `Indent`, but changing whether to insert
tabs or spaces. there is also the `-s` option to enable it on startup.

The number of spaces to insert is the same as the tab width,
controlled by `Tab`.

My implementation may be a bit awkward, I haven't tried modifying acme
to this extent before.
-- 
binarycat <dogedoge61@gmail.com>

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Td96032ea2eda50a5-M4893eb0b25b35bb71504e5b3
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/x-patch, Size: 5245 bytes --]

From c60b2b2a0dc502e61ab7d80b3ef5d0aa8b37690b Mon Sep 17 00:00:00 2001
From: binarycat <dogedoge61@gmail.com>
Date: Thu, 5 Aug 2021 12:48:08 -0400
Subject: [PATCH] spaceindent

---
 acme.c |  5 ++++-
 dat.h  |  2 ++
 exec.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 text.c |  7 +++++++
 wind.c |  1 +
 5 files changed, 64 insertions(+), 6 deletions(-)

diff --git a/acme.c b/acme.c
index d001a2a..d0c1772 100644
--- a/acme.c
+++ b/acme.c
@@ -114,6 +114,9 @@ threadmain(int argc, char *argv[])
 	case 'r':
 		swapscrollbuttons = TRUE;
 		break;
+	case 's':
+		globalspaceindent = TRUE;
+		break;
 	case 'W':
 		winsize = ARGF();
 		if(winsize == nil)
@@ -121,7 +124,7 @@ threadmain(int argc, char *argv[])
 		break;
 	default:
 	Usage:
-		fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
+		fprint(2, "usage: acme -as -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
 		threadexitsall("usage");
 	}ARGEND
 
diff --git a/dat.h b/dat.h
index 8a81c97..90a3794 100644
--- a/dat.h
+++ b/dat.h
@@ -241,6 +241,7 @@ struct Window
 	uchar	filemenu;
 	uchar	dirty;
 	uchar	autoindent;
+	uchar	spaceindent;
 	uchar	showdel;
 	int		id;
 	Range	addr;
@@ -553,6 +554,7 @@ int			editing;
 int			erroutfd;
 int			messagesize;		/* negotiated in 9P version setup */
 int			globalautoindent;
+int			globalspaceindent;
 int			dodollarsigns;
 char*		mtpt;
 
diff --git a/exec.c b/exec.c
index be7936a..547e988 100644
--- a/exec.c
+++ b/exec.c
@@ -55,6 +55,7 @@ void	sendx(Text*, Text*, Text*, int, int, Rune*, int);
 void	sort(Text*, Text*, Text*, int, int, Rune*, int);
 void	tab(Text*, Text*, Text*, int, int, Rune*, int);
 void	zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
+void spaceindent(Text*, Text*, Text*, int, int, Rune*, int);
 
 typedef struct Exectab Exectab;
 struct Exectab
@@ -95,6 +96,8 @@ static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
 static Rune LTab[] = { 'T', 'a', 'b', 0 };
 static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
 static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
+static Rune LSpaceindent[] = { 'S', 'p', 'a', 'c', 'e', 'i', 'n', 'd', 'e', 'n', 't', 0 };
+
 
 Exectab exectab[] = {
 	{ LAbort,		doabort,	FALSE,	XXX,		XXX,		},
@@ -110,6 +113,7 @@ Exectab exectab[] = {
 	{ LID,		id,		FALSE,	XXX,		XXX		},
 	{ LIncl,		incl,		FALSE,	XXX,		XXX		},
 	{ LIndent,		indent,	FALSE,	XXX,		XXX		},
+	{ LSpaceindent,	spaceindent,	FALSE,	XXX,	XXX	},
 	{ LKill,		xkill,		FALSE,	XXX,		XXX		},
 	{ LLoad,		dump,	FALSE,	FALSE,	XXX		},
 	{ LLocal,		local,	FALSE,	XXX,		XXX		},
@@ -1395,29 +1399,37 @@ enum {
 	Ioff = 1
 };
 
+
+
 static int
-indentval(Rune *s, int n)
+onoffval(Rune *s, int n, int *global)
 {
 	if(n < 2)
 		return IError;
 	if(runestrncmp(s, LON, n) == 0){
-		globalautoindent = TRUE;
-		warning(nil, "Indent ON\n");
+		*global = TRUE;
+		//warning(nil, "Indent ON\n");
 		return IGlobal;
 	}
 	if(runestrncmp(s, LOFF, n) == 0){
-		globalautoindent = FALSE;
-		warning(nil, "Indent OFF\n");
+		*global = FALSE;
+		//warning(nil, "Indent OFF\n");
 		return IGlobal;
 	}
 	return runestrncmp(s, Lon, n) == 0;
 }
 
+static int
+indentval(Rune *s, int n) {
+	return onoffval(s, n, &globalautoindent);
+}
+
 static void
 fixindent(Window *w, void *arg)
 {
 	USED(arg);
 	w->autoindent = globalautoindent;
+	w->spaceindent = globalspaceindent;
 }
 
 void
@@ -1426,6 +1438,8 @@ indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
 	Rune *a, *r;
 	Window *w;
 	int na, len, autoindent;
+	
+	
 
 	USED(_0);
 	USED(_1);
@@ -1449,6 +1463,37 @@ indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
 		w->autoindent = autoindent;
 }
 
+void
+spaceindent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
+{
+	Rune *a, *r;
+	Window *w;
+	int na, len, spindent;
+	
+	
+
+	USED(_0);
+	USED(_1);
+	USED(_2);
+
+	w = nil;
+	if(et!=nil && et->w!=nil)
+		w = et->w;
+	spindent = IError;
+	getarg(argt, FALSE, TRUE, &r, &len);
+	if(r!=nil && len>0)
+		spindent = onoffval(r, len, &globalspaceindent);
+	else{ /* I'm not really sure what this does */
+		a = findbl(arg, narg, &na);
+		if(a != arg)
+			spindent = onoffval(arg, narg-na, &globalspaceindent);
+	}
+	if(spindent == IGlobal)
+		allwindows(fixindent, nil);
+	else if(w != nil && spindent >= 0)
+		w->spaceindent = spindent;
+}
+
 void
 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
 {
diff --git a/text.c b/text.c
index 09422dd..01730e0 100644
--- a/text.c
+++ b/text.c
@@ -902,6 +902,13 @@ texttype(Text *t, Rune r)
 			}
 		}
 		break; /* fall through to normal code */
+	case '\t':
+		if(t->w->spaceindent){
+			for(i=0; i<t->w->body.tabstop; i++)
+				texttype(t, ' ');
+			return;
+		}
+		break;
 	}
 	/* otherwise ordinary character; just insert, typically in caches of all texts */
 	for(i=0; i<t->file->ntext; i++){
diff --git a/wind.c b/wind.c
index 0cba592..abeacf0 100644
--- a/wind.c
+++ b/wind.c
@@ -81,6 +81,7 @@ wininit(Window *w, Window *clone, Rectangle r)
 	w->filemenu = TRUE;
 	w->maxlines = w->body.fr.maxlines;
 	w->autoindent = globalautoindent;
+	w->spaceindent = globalspaceindent;
 	if(clone){
 		w->dirty = clone->dirty;
 		w->autoindent = clone->autoindent;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-06 17:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-05 16:59 [9fans] [PATCH] Spaceindent mode for acme binarycat
2021-08-05 17:28 ` Rodrigo G. López
2021-08-06 17:15   ` theinicke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).