shithub: front

Download patch

ref: 12ad6dffeb56a2ac3b63c13528534767d57d03e7
parent: 3d6deb3f690c2f5e68325c8c528216b386606f63
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Jun 16 11:58:07 EDT 2024

sam: make X/.../b work

--- a/sys/src/cmd/sam/sam.c
+++ b/sys/src/cmd/sam/sam.c
@@ -5,7 +5,6 @@
 int	panicking;
 int	rescuing;
 String	genstr;
-String	rhs;
 String	curwd;
 String	cmdstr;
 Rune	empty[] = { 0 };
@@ -29,8 +28,6 @@
 Disk	*disk;
 long	seq;
 
-Rune	baddir[] = { '<', 'b', 'a', 'd', 'd', 'i', 'r', '>', '\n'};
-
 void	usage(void);
 
 void main(int argc, char *argv[])
@@ -78,7 +75,6 @@
 	Strinit0(&lastpat);
 	Strinit0(&lastregexp);
 	Strinit0(&genstr);
-	Strinit0(&rhs);
 	Strinit0(&curwd);
 	Strinit0(&plan9cmd);
 	home = getenv(HOME);
@@ -538,20 +534,20 @@
 }
 
 int
-loadflist(String *s)
+loadflist(String *s, int blank)
 {
 	int c, i;
 
 	c = s->s[0];
-	for(i = 0; s->s[i]==' ' || s->s[i]=='\t'; i++)
+	for(i = 0; i < s->n && (s->s[i]==' ' || s->s[i]=='\t'); i++)
 		;
-	if((c==' ' || c=='\t') && s->s[i]!='\n'){
+	if(blank == 0 || ((c==' ' || c=='\t') && s->s[i]!='\n')){
 		if(s->s[i]=='<'){
 			Strdelete(s, 0L, (long)i+1);
 			readcmd(s);
 		}else{
 			Strzero(&genstr);
-			while((c = s->s[i++]) && c!='\n')
+			while(i < s->n && (c = s->s[i++]) && c!='\n')
 				Straddc(&genstr, c);
 			Straddc(&genstr, '\0');
 		}
@@ -604,13 +600,13 @@
 }
 
 File *
-tofile(String *s)
+tofile(String *s, int blank)
 {
 	File *f;
 
-	if(s->s[0] != ' ')
+	if(blank && s->s[0] != ' ')
 		error(Eblank);
-	if(loadflist(s) == 0){
+	if(loadflist(s, blank) == 0){
 		f = lookfile(&genstr);	/* empty string ==> nameless file */
 		if(f == 0)
 			error_s(Emenu, genc);
@@ -624,7 +620,7 @@
 {
 	File *f;
 
-	if(loadflist(s) == 0)
+	if(loadflist(s, 1) == 0)
 		logsetname(f = newfile(), &genstr);
 	else if((f=readflist(TRUE, FALSE)) == 0)
 		error(Eblank);
@@ -642,7 +638,7 @@
 	}
 	if(s->s[0] != ' ')
 		error(Eblank);
-	if(loadflist(s) == 0)
+	if(loadflist(s, 1) == 0)
 		error(Enewline);
 	readflist(FALSE, TRUE);
 }
--- a/sys/src/cmd/sam/sam.h
+++ b/sys/src/cmd/sam/sam.h
@@ -316,7 +316,7 @@
 void	freetmpstr(String*);
 void	termcommand(void);
 void	termwrite(char*);
-File	*tofile(String*);
+File	*tofile(String*, int);
 void	trytoclose(File*);
 void	trytoquit(void);
 int	undo(int);
--- a/sys/src/cmd/sam/xec.c
+++ b/sys/src/cmd/sam/xec.c
@@ -3,6 +3,7 @@
 
 int	Glooping;
 int	nest;
+int	newcur;
 
 int	append(File*, Cmd*, Posn);
 int	display(File*);
@@ -77,11 +78,21 @@
 int
 b_cmd(File *f, Cmd *cp)
 {
+	String *s;
 	USED(f);
-	f = cp->cmdc=='b'? tofile(cp->ctext) : getfile(cp->ctext);
+	s = cp->ctext;
+	if(nest > 0 && s->s[0] == 0){
+		if(f == nil)
+			return TRUE;
+		tofile(&f->name, 0);
+		current(f);
+		newcur = 1;
+	}else{
+		f = cp->cmdc=='b'? tofile(s, 1) : getfile(s);
+	}
 	if(f->unread)
 		load(f);
-	else if(nest == 0)
+	else if(nest == 0 || newcur)
 		filename(f);
 	return TRUE;
 }
@@ -506,6 +517,7 @@
 	nest++;
 	settempfile();
 	cur = curfile;
+	newcur = 0;
 	for(i = 0; i<tempfile.nused; i++){
 		f = tempfile.filepptr[i];
 		if(f==cmd)
@@ -513,7 +525,7 @@
 		if(cp->re==0 || filematch(f, cp->re)==XY)
 			cmdexec(f, cp->ccmd);
 	}
-	if(cur && whichmenu(cur)>=0)	/* check that cur is still a file */
+	if(newcur == 0 && cur && whichmenu(cur)>=0)	/* check that cur is still a file */
 		current(cur);
 	--Glooping;
 	--nest;
--