From: lum Date: Tue, 20 Apr 2021 10:02:50 +0000 (+0000) Subject: Add 'dired-jump' from Philip K. sent to tech@ X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=cc1acc7af70026cf8a473f633453b56418738c8f;p=openbsd Add 'dired-jump' from Philip K. sent to tech@ over a year ago. Comments and testing from gkoehler@. Thanks to both. Also, some modifications from me. --- diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index c224f86c29a..f5102b840ac 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.171 2021/03/25 12:46:11 lum Exp $ */ +/* $OpenBSD: def.h,v 1.172 2021/04/20 10:02:50 lum Exp $ */ /* This file is in the public domain. */ @@ -363,6 +363,7 @@ int ask_makedir(void); /* dired.c */ struct buffer *dired_(char *); +int dired_jump(int, int); int do_dired(char *); /* file.c X */ diff --git a/usr.bin/mg/dired.c b/usr.bin/mg/dired.c index 3337cd4a028..c436bf7e6b2 100644 --- a/usr.bin/mg/dired.c +++ b/usr.bin/mg/dired.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dired.c,v 1.98 2021/03/05 16:16:53 lum Exp $ */ +/* $OpenBSD: dired.c,v 1.99 2021/04/20 10:02:50 lum Exp $ */ /* This file is in the public domain. */ @@ -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 *); @@ -1090,14 +1091,51 @@ createlist(struct buffer *bp) return (ret); } +int +dired_jump(int f, int n) +{ + struct buffer *bp; + const char *modename; + char dname[NFILEN], *fname; + int ret, i; + + /* + * We use fundamental mode in dired, so just check we aren't in + * dired mode for this specific function. Seems like a corner + * case at the moment. + */ + for (i = 0; i <= curbp->b_nmodes; i++) { + modename = curbp->b_modes[i]->p_name; + if (strncmp(modename, "dired", 5) == 0) + return (dobeep_msg("In dired mode already")); + } + + if (getbufcwd(dname, sizeof(dname)) != TRUE) + return (FALSE); + + fname = curbp->b_fname; + + if ((bp = dired_(dname)) == NULL) + return (FALSE); + curbp = bp; + + ret = showbuffer(bp, curwp, WFFULL | WFMODE); + if (ret != TRUE) + return ret; + + fname = adjustname(fname, TRUE); + if (fname != NULL) + gotofile(fname); + + return (TRUE); +} + int d_gotofile(int f, int n) { - struct line *lp, *nlp; size_t lenfpath; - char fpath[NFILEN], fname[NFILEN]; - char *p, *fpth, *fnp = NULL; - int tmp; + char fpath[NFILEN]; + char *fpth, *fnp = NULL; if (getbufcwd(fpath, sizeof(fpath)) != TRUE) fpath[0] = '\0'; @@ -1114,8 +1152,18 @@ d_gotofile(int f, int n) ewprintf("No file to find"); /* Current directory given so */ return (TRUE); /* return at present location. */ } + return gotofile(fpth); +} + +int +gotofile(char *fpth) +{ + struct line *lp, *nlp; + char fname[NFILEN]; + char *p; + int tmp; + (void)xbasename(fname, fpth, NFILEN); - curbp = curwp->w_bufp; tmp = 0; for (lp = bfirstlp(curbp); lp != curbp->b_headp; lp = nlp) { tmp++; diff --git a/usr.bin/mg/funmap.c b/usr.bin/mg/funmap.c index 14b6ea4ee53..b9207c6b772 100644 --- a/usr.bin/mg/funmap.c +++ b/usr.bin/mg/funmap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: funmap.c,v 1.60 2021/03/23 18:33:05 lum Exp $ */ +/* $OpenBSD: funmap.c,v 1.61 2021/04/20 10:02:50 lum Exp $ */ /* This file is in the public domain */ @@ -98,6 +98,7 @@ static struct funmap functnames[] = { {desckey, "describe-key-briefly", 1}, {diffbuffer, "diff-buffer-with-file", 0}, {digit_argument, "digit-argument", 1}, + {dired_jump, "dired-jump", 1}, {lowerregion, "downcase-region", 0}, {lowerword, "downcase-word", 1}, {showversion, "emacs-version", 0}, diff --git a/usr.bin/mg/keymap.c b/usr.bin/mg/keymap.c index ef71f845db9..32a9267d30f 100644 --- a/usr.bin/mg/keymap.c +++ b/usr.bin/mg/keymap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: keymap.c,v 1.58 2015/12/29 19:44:32 lum Exp $ */ +/* $OpenBSD: keymap.c,v 1.59 2021/04/20 10:02:50 lum Exp $ */ /* This file is in the public domain. */ @@ -129,7 +129,9 @@ static PF cXcB[] = { ctrlg /* ^G */ }; -static PF cXcL[] = { +static PF cXcJ[] = { + dired_jump, /* ^J */ + rescan, /* ^K */ lowerregion, /* ^L */ rescan, /* ^M */ rescan, /* ^N */ @@ -198,7 +200,7 @@ struct KEYMAPE (6) cXmap = { CCHR('B'), CCHR('G'), cXcB, NULL }, { - CCHR('L'), CCHR('X'), cXcL, NULL + CCHR('J'), CCHR('X'), cXcJ, NULL }, { '(', ')', cXlp, NULL diff --git a/usr.bin/mg/mg.1 b/usr.bin/mg/mg.1 index a5a7f9aae45..e43d851b513 100644 --- a/usr.bin/mg/mg.1 +++ b/usr.bin/mg/mg.1 @@ -1,7 +1,7 @@ -.\" $OpenBSD: mg.1,v 1.122 2021/03/26 15:02:10 lum Exp $ +.\" $OpenBSD: mg.1,v 1.123 2021/04/20 10:02:50 lum Exp $ .\" This file is in the public domain. .\" -.Dd $Mdocdate: March 26 2021 $ +.Dd $Mdocdate: April 20 2021 $ .Dt MG 1 .Os .Sh NAME @@ -205,6 +205,8 @@ list-buffers save-buffers-kill-emacs .It C-x C-f find-file +.It C-x C-j +dired-jump .It C-x C-g keyboard-quit .It C-x C-l @@ -530,6 +532,8 @@ Display the name of the function currently bound to the key. View the differences between buffer and its associated file. .It digit-argument Process a numerical argument for keyboard-invoked functions. +.It dired-jump +Open a dired buffer containing the current buffer's directory location. .It downcase-region Set all characters in the region to lower case. .It downcase-word