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 -0000 1.166
+++ def.h 30 Mar 2020 13:04:20 -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 30 Mar 2020 13:04:20 -0000
@@ -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;
+ int len;
+
+ 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 -0000 1.58
+++ keymap.c 30 Mar 2020 13:04:20 -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