George Koehler <kern...@gmail.com> 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 -0000	1.166
+++ def.h	1 Apr 2020 11:42:45 -0000
@@ -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 -0000	1.93
+++ dired.c	1 Apr 2020 11:42:45 -0000
@@ -17,6 +17,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
+#include <libgen.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -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;
+	int				len, 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 -0000	1.58
+++ keymap.c	1 Apr 2020 11:42:45 -0000
@@ -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 KEYMAPE (6) cXmap = {
-	6,
-	6,
+struct KEYMAPE (7) cXmap = {
+	7,
+	7,
 	rescan,
 	{
 		{
 			CCHR('B'), CCHR('G'), cXcB, NULL
+		},
+		{
+			CCHR('J'), CCHR('J'), cXcJ, NULL
 		},
 		{
 			CCHR('L'), CCHR('X'), cXcL, NULL

Reply via email to