Re: [PATCH] dired-jump for mg

2020-04-13 Thread Philip K.
George Koehler  writes:

> In emacs, if I visit /tmp/nowhere/new, but /tmp/nowhere doesn't exist,
> then C-x C-j shows an error:
> | Setting current directory: No such file or directory, /tmp/nowhere/
>
> In mg with your diff, if I do the same, then C-x C-j does nothing, and
> doesn't show an error.  I would like to see an error.  I don't need the
> exact same error message as emacs; but I want to know that mg got my
> command, and the command failed.  (For people trying emacs, C-x C-j
> doesn't work until you load dired-x.  An easy way to load dired-x is
> to visit any dir, then M-x dired-jump.)

Good catch, didn't think about that. Added an error message using
dobeep_msgs. See patch below.

-- 
Philip K.

? dired-jump.patch
cvs server: Diffing .
Index: def.h
===
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.166
diff -u -r1.166 def.h
--- def.h	9 Feb 2020 10:13:13 -	1.166
+++ def.h	13 Apr 2020 11:34:18 -
@@ -361,6 +361,7 @@
 
 /* dired.c */
 struct buffer	*dired_(char *);
+int 		 dired_jump(int, int);
 int 		 do_dired(char *);
 
 /* file.c X */
Index: dired.c
===
RCS file: /cvs/src/usr.bin/mg/dired.c,v
retrieving revision 1.93
diff -u -r1.93 dired.c
--- dired.c	11 Jul 2019 18:20:18 -	1.93
+++ dired.c	13 Apr 2020 11:34:18 -
@@ -17,6 +17,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -53,6 +54,7 @@
 static int	 d_filevisitalt(int, int);
 static int	 d_gotofile(int, int);
 static void	 reaper(int);
+static int	 gotofile(char*);
 static struct buffer	*refreshbuffer(struct buffer *);
 static int	 createlist(struct buffer *);
 static void	 redelete(struct buffer *);
@@ -205,6 +207,7 @@
 dired_init(void)
 {
 	funmap_add(dired, "dired", 1);
+	funmap_add(dired_jump, "dired-jump", 1);
 	funmap_add(d_create_directory, "dired-create-directory", 1);
 	funmap_add(d_copy, "dired-do-copy", 1);
 	funmap_add(d_expunge, "dired-do-flagged-delete", 0);
@@ -257,6 +260,40 @@
 
 /* ARGSUSED */
 int
+dired_jump(int f, int n)
+{
+	char		 dname[NFILEN], base[NFILEN], *fname;
+	struct buffer	*bp;
+	intlen, ret;
+
+	if (getbufcwd(dname, sizeof(dname)) != TRUE)
+		return (FALSE);
+
+	fname = curbp->b_fname;
+
+	if ((bp = dired_(dname)) == FALSE) {
+		ewprintf("Cannot find directory: %s", dname);
+		return (FALSE);
+	}
+	curbp = bp;
+
+	ret = showbuffer(bp, curwp, WFFULL | WFMODE);
+	if (ret != (TRUE))
+		 return ret;
+
+	if (fname[0]) {
+		len = strlen(fname);
+		if (fname[len-1] != '/') {
+			(void) xbasename(base, fname, NFILEN);
+			gotofile(base);
+		}
+	}
+
+	return (TRUE);
+}
+
+/* ARGSUSED */
+int
 d_otherwindow(int f, int n)
 {
 	char		 dname[NFILEN], *bufp, *slash;
@@ -1079,35 +1116,15 @@
 }
 
 int
-d_gotofile(int f, int n)
+gotofile(char *fname)
 {
 	struct line	*lp, *nlp;
-	struct buffer   *curbp;
-	size_t		 lenfpath;
-	char		 fpath[NFILEN], fname[NFILEN];
-	char		*p, *fpth, *fnp = NULL;
+	char	 *p;
 	int		 tmp;
 
-	if (getbufcwd(fpath, sizeof(fpath)) != TRUE)
-		fpath[0] = '\0';
-	lenfpath = strlen(fpath);
-	fnp = eread("Goto file: ", fpath, NFILEN,
-	EFNEW | EFCR | EFFILE | EFDEF);
-	if (fnp == NULL)
-		return (ABORT);
-	else if (fnp[0] == '\0')
-		return (FALSE);
-
-	fpth = adjustname(fpath, TRUE);		/* Removes last '/' if	*/
-	if (strlen(fpth) == lenfpath - 1) {	/* directory, hence -1.	*/
-		ewprintf("No file to find");	/* Current directory given so  */
-		return (TRUE);			/* return at present location. */
-	}
-	(void)xbasename(fname, fpth, NFILEN);
-	curbp = curwp->w_bufp;
 	tmp = 0;
-	for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
-		tmp++;
+ 	for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
+ 		tmp++;
 		if ((p = findfname(lp, p)) == NULL) {
 			nlp = lforw(lp);
 			continue;
@@ -1128,6 +1145,34 @@
 		ewprintf("");
 		return (TRUE);
 	}
+}
+
+int
+d_gotofile(int f, int n)
+{
+	size_t		 lenfpath;
+	char		 fpath[NFILEN], fname[NFILEN];
+	char		 *fpth, *fnp = NULL;
+
+	if (getbufcwd(fpath, sizeof(fpath)) != TRUE)
+		fpath[0] = '\0';
+	lenfpath = strlen(fpath);
+	fnp = eread("Goto file: ", fpath, NFILEN,
+	EFNEW | EFCR | EFFILE | EFDEF);
+	if (fnp == NULL)
+		return (ABORT);
+	else if (fnp[0] == '\0')
+		return (FALSE);
+
+	fpth = adjustname(fpath, TRUE);		/* Removes last '/' if	*/
+	if (strlen(fpth) == lenfpath - 1) {	/* directory, hence -1.	*/
+		ewprintf("No file to find");	/* Current directory given so  */
+		return (TRUE);			/* return at present location. */
+	}
+	(void)xbasename(fname, fpth, NFILEN);
+	curbp = curwp->w_bufp;
+
+	return gotofile(fname);
 }
 
 /*
Index: keymap.c
===
RCS file: /cvs/src/usr.bin/mg/keymap.c,v
retrieving revision 1.58
diff -u -r1.58 

Re: [PATCH] dired-jump for mg

2020-04-01 Thread Philip K.
George Koehler  writes:

> C-x C-j doesn't work for me: it does show the dired buffer, but
> doesn't jump to the file that I was editing.

Looks like that was because I called gotofile before showbuffer. I
changed that before submitting it, and I remember it seeming to work,
but I guess I was mistaken :/ This patch should fix that though.

-- 
    Philip K.

? dired-jump.patch
cvs server: Diffing .
Index: def.h
===
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.166
diff -u -p -r1.166 def.h
--- def.h	9 Feb 2020 10:13:13 -	1.166
+++ def.h	1 Apr 2020 11:42:45 -
@@ -361,6 +361,7 @@ int		 ask_makedir(void);
 
 /* dired.c */
 struct buffer	*dired_(char *);
+int 		 dired_jump(int, int);
 int 		 do_dired(char *);
 
 /* file.c X */
Index: dired.c
===
RCS file: /cvs/src/usr.bin/mg/dired.c,v
retrieving revision 1.93
diff -u -p -r1.93 dired.c
--- dired.c	11 Jul 2019 18:20:18 -	1.93
+++ dired.c	1 Apr 2020 11:42:45 -
@@ -17,6 +17,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -53,6 +54,7 @@ static int	 d_refreshbuffer(int, int);
 static int	 d_filevisitalt(int, int);
 static int	 d_gotofile(int, int);
 static void	 reaper(int);
+static int	 gotofile(char*);
 static struct buffer	*refreshbuffer(struct buffer *);
 static int	 createlist(struct buffer *);
 static void	 redelete(struct buffer *);
@@ -205,6 +207,7 @@ void
 dired_init(void)
 {
 	funmap_add(dired, "dired", 1);
+	funmap_add(dired_jump, "dired-jump", 1);
 	funmap_add(d_create_directory, "dired-create-directory", 1);
 	funmap_add(d_copy, "dired-do-copy", 1);
 	funmap_add(d_expunge, "dired-do-flagged-delete", 0);
@@ -257,6 +260,36 @@ dired(int f, int n)
 
 /* ARGSUSED */
 int
+dired_jump(int f, int n)
+{
+	char		 dname[NFILEN], *fname;
+	struct buffer	*bp;
+	intlen, ret;
+
+	if (getbufcwd(dname, sizeof(dname)) != TRUE)
+		return (FALSE);
+
+	fname = curbp->b_fname;
+
+	if ((bp = dired_(dname)) == FALSE)
+		return (FALSE);
+	curbp = bp;
+
+	ret = showbuffer(bp, curwp, WFFULL | WFMODE);
+	if (ret != (TRUE))
+		 return ret;
+
+	if (fname[0]) {
+		len = strlen(fname);
+		if (fname[len-1] != '/')
+			gotofile(basename(fname));
+	}
+
+	return (TRUE);
+}
+
+/* ARGSUSED */
+int
 d_otherwindow(int f, int n)
 {
 	char		 dname[NFILEN], *bufp, *slash;
@@ -1079,35 +1112,15 @@ createlist(struct buffer *bp)
 }
 
 int
-d_gotofile(int f, int n)
+gotofile(char *fname)
 {
 	struct line	*lp, *nlp;
-	struct buffer   *curbp;
-	size_t		 lenfpath;
-	char		 fpath[NFILEN], fname[NFILEN];
-	char		*p, *fpth, *fnp = NULL;
+	char	 *p;
 	int		 tmp;
 
-	if (getbufcwd(fpath, sizeof(fpath)) != TRUE)
-		fpath[0] = '\0';
-	lenfpath = strlen(fpath);
-	fnp = eread("Goto file: ", fpath, NFILEN,
-	EFNEW | EFCR | EFFILE | EFDEF);
-	if (fnp == NULL)
-		return (ABORT);
-	else if (fnp[0] == '\0')
-		return (FALSE);
-
-	fpth = adjustname(fpath, TRUE);		/* Removes last '/' if	*/
-	if (strlen(fpth) == lenfpath - 1) {	/* directory, hence -1.	*/
-		ewprintf("No file to find");	/* Current directory given so  */
-		return (TRUE);			/* return at present location. */
-	}
-	(void)xbasename(fname, fpth, NFILEN);
-	curbp = curwp->w_bufp;
 	tmp = 0;
-	for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
-		tmp++;
+ 	for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
+ 		tmp++;
 		if ((p = findfname(lp, p)) == NULL) {
 			nlp = lforw(lp);
 			continue;
@@ -1128,6 +1141,34 @@ d_gotofile(int f, int n)
 		ewprintf("");
 		return (TRUE);
 	}
+}
+
+int
+d_gotofile(int f, int n)
+{
+	size_t		 lenfpath;
+	char		 fpath[NFILEN], fname[NFILEN];
+	char		 *fpth, *fnp = NULL;
+
+	if (getbufcwd(fpath, sizeof(fpath)) != TRUE)
+		fpath[0] = '\0';
+	lenfpath = strlen(fpath);
+	fnp = eread("Goto file: ", fpath, NFILEN,
+	EFNEW | EFCR | EFFILE | EFDEF);
+	if (fnp == NULL)
+		return (ABORT);
+	else if (fnp[0] == '\0')
+		return (FALSE);
+
+	fpth = adjustname(fpath, TRUE);		/* Removes last '/' if	*/
+	if (strlen(fpth) == lenfpath - 1) {	/* directory, hence -1.	*/
+		ewprintf("No file to find");	/* Current directory given so  */
+		return (TRUE);			/* return at present location. */
+	}
+	(void)xbasename(fname, fpth, NFILEN);
+	curbp = curwp->w_bufp;
+
+	return gotofile(fname);
 }
 
 /*
Index: keymap.c
===
RCS file: /cvs/src/usr.bin/mg/keymap.c,v
retrieving revision 1.58
diff -u -p -r1.58 keymap.c
--- keymap.c	29 Dec 2015 19:44:32 -	1.58
+++ keymap.c	1 Apr 2020 11:42:45 -
@@ -129,6 +129,10 @@ static PF cXcB[] = {
 	ctrlg			/* ^G */
 };
 
+static PF cXcJ[] = {
+	dired_jump		/* ^J */
+};
+
 static PF cXcL[] = {
 	lowerregion,		/* ^L */
 	rescan,			/* ^M */
@@ -189,13 +193,16 @@ static PF cXcar[] = {
 	undo			/* u */
 };
 
-struct 

[PATCH] dired-jump for mg

2020-03-30 Thread Philip K.

Hi,

this patch implements "dired-jump" for mg. For those not familiar with
what dired-jump does in GNU Emacs, here's it's docstring:

Jump to Dired buffer corresponding to current buffer.
If in a file, Dired the current directory and move to file’s line.

I apologise in case there are any problems applying this patch, I'm
(sadly) not a OpenBSD user so I developed the change using the Linux
port and then copied the changes. I optimistically assumed that this
should work, but in case there are any issues, I'll spin up a VM and
debug it.

-- 
    Philip K.

? dired-jump.patch
cvs server: Diffing .
Index: def.h
===
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.166
diff -u -p -r1.166 def.h
--- def.h	9 Feb 2020 10:13:13 -	1.166
+++ def.h	30 Mar 2020 13:04:20 -
@@ -361,6 +361,7 @@ int		 ask_makedir(void);
 
 /* dired.c */
 struct buffer	*dired_(char *);
+int 		 dired_jump(int, int);
 int 		 do_dired(char *);
 
 /* file.c X */
Index: dired.c
===
RCS file: /cvs/src/usr.bin/mg/dired.c,v
retrieving revision 1.93
diff -u -p -r1.93 dired.c
--- dired.c	11 Jul 2019 18:20:18 -	1.93
+++ dired.c	30 Mar 2020 13:04:20 -
@@ -53,6 +53,7 @@ static int	 d_refreshbuffer(int, int);
 static int	 d_filevisitalt(int, int);
 static int	 d_gotofile(int, int);
 static void	 reaper(int);
+static int	 gotofile(char*);
 static struct buffer	*refreshbuffer(struct buffer *);
 static int	 createlist(struct buffer *);
 static void	 redelete(struct buffer *);
@@ -205,6 +206,7 @@ void
 dired_init(void)
 {
 	funmap_add(dired, "dired", 1);
+	funmap_add(dired_jump, "dired-jump", 1);
 	funmap_add(d_create_directory, "dired-create-directory", 1);
 	funmap_add(d_copy, "dired-do-copy", 1);
 	funmap_add(d_expunge, "dired-do-flagged-delete", 0);
@@ -257,6 +259,32 @@ dired(int f, int n)
 
 /* ARGSUSED */
 int
+dired_jump(int f, int n)
+{
+	char		 dname[NFILEN], *fname;
+	struct buffer	*bp;
+	intlen;
+
+	if (getbufcwd(dname, sizeof(dname)) != TRUE)
+		return (FALSE);
+
+	fname = curbp->b_fname;
+
+	if ((bp = dired_(dname)) == FALSE)
+		return (FALSE);
+	curbp = bp;
+
+	if (fname[0]) {
+		len = strlen(fname);
+		if (fname[len-1] != '/')
+			gotofile(basename(fname));
+	}
+
+	return showbuffer(bp, curwp, WFFULL | WFMODE);
+}
+
+/* ARGSUSED */
+int
 d_otherwindow(int f, int n)
 {
 	char		 dname[NFILEN], *bufp, *slash;
@@ -1079,35 +1107,15 @@ createlist(struct buffer *bp)
 }
 
 int
-d_gotofile(int f, int n)
+gotofile(char *fname)
 {
 	struct line	*lp, *nlp;
-	struct buffer   *curbp;
-	size_t		 lenfpath;
-	char		 fpath[NFILEN], fname[NFILEN];
-	char		*p, *fpth, *fnp = NULL;
+	char	 *p;
 	int		 tmp;
 
-	if (getbufcwd(fpath, sizeof(fpath)) != TRUE)
-		fpath[0] = '\0';
-	lenfpath = strlen(fpath);
-	fnp = eread("Goto file: ", fpath, NFILEN,
-	EFNEW | EFCR | EFFILE | EFDEF);
-	if (fnp == NULL)
-		return (ABORT);
-	else if (fnp[0] == '\0')
-		return (FALSE);
-
-	fpth = adjustname(fpath, TRUE);		/* Removes last '/' if	*/
-	if (strlen(fpth) == lenfpath - 1) {	/* directory, hence -1.	*/
-		ewprintf("No file to find");	/* Current directory given so  */
-		return (TRUE);			/* return at present location. */
-	}
-	(void)xbasename(fname, fpth, NFILEN);
-	curbp = curwp->w_bufp;
 	tmp = 0;
-	for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
-		tmp++;
+ 	for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) {
+ 		tmp++;
 		if ((p = findfname(lp, p)) == NULL) {
 			nlp = lforw(lp);
 			continue;
@@ -1128,6 +1136,34 @@ d_gotofile(int f, int n)
 		ewprintf("");
 		return (TRUE);
 	}
+}
+
+int
+d_gotofile(int f, int n)
+{
+	size_t		 lenfpath;
+	char		 fpath[NFILEN], fname[NFILEN];
+	char		 *fpth, *fnp = NULL;
+
+	if (getbufcwd(fpath, sizeof(fpath)) != TRUE)
+		fpath[0] = '\0';
+	lenfpath = strlen(fpath);
+	fnp = eread("Goto file: ", fpath, NFILEN,
+	EFNEW | EFCR | EFFILE | EFDEF);
+	if (fnp == NULL)
+		return (ABORT);
+	else if (fnp[0] == '\0')
+		return (FALSE);
+
+	fpth = adjustname(fpath, TRUE);		/* Removes last '/' if	*/
+	if (strlen(fpth) == lenfpath - 1) {	/* directory, hence -1.	*/
+		ewprintf("No file to find");	/* Current directory given so  */
+		return (TRUE);			/* return at present location. */
+	}
+	(void)xbasename(fname, fpth, NFILEN);
+	curbp = curwp->w_bufp;
+
+	return gotofile(fname);
 }
 
 /*
Index: keymap.c
===
RCS file: /cvs/src/usr.bin/mg/keymap.c,v
retrieving revision 1.58
diff -u -p -r1.58 keymap.c
--- keymap.c	29 Dec 2015 19:44:32 -	1.58
+++ keymap.c	30 Mar 2020 13:04:20 -
@@ -129,6 +129,10 @@ static PF cXcB[] = {
 	ctrlg			/* ^G */
 };
 
+static PF cXcJ[] = {
+	dired_jump		/* ^J */
+};
+
 static PF cXcL[] = {
 	lowerregion,		/* ^L */
 	rescan,			/* ^M */
@@ -189