Re: mg(1): dired-jump

2021-03-19 Thread Mark Lumsden

Should this be "if (fname != NULL)"?  Some other callers of
adjustname() check for a NULL pointer.  strlen(NULL) would crash by
SIGSEGV; strlen("") would return 0, because "" is a non-NULL pointer
to an ASCII NUL '\0'.


I agree. Amended diff below.

Index: def.h
===
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.168
diff -u -p -r1.168 def.h
--- def.h   1 Mar 2021 10:51:14 -   1.168
+++ def.h   19 Mar 2021 06:02:05 -
@@ -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 */
Index: dired.c
===
RCS file: /cvs/src/usr.bin/mg/dired.c,v
retrieving revision 1.98
diff -u -p -r1.98 dired.c
--- dired.c 5 Mar 2021 16:16:53 -   1.98
+++ dired.c 19 Mar 2021 06:02:05 -
@@ -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 *);
@@ -1091,13 +1092,38 @@ createlist(struct buffer *bp)
 }

 int
+dired_jump(int f, int n)
+{
+   char dname[NFILEN], *fname;
+   struct buffer   *bp;
+   int  ret;
+
+   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 +1140,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++;
Index: funmap.c
===
RCS file: /cvs/src/usr.bin/mg/funmap.c,v
retrieving revision 1.59
diff -u -p -r1.59 funmap.c
--- funmap.c11 Jul 2019 18:20:18 -  1.59
+++ funmap.c19 Mar 2021 06:02:05 -
@@ -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", 0},
{showversion, "emacs-version", 0},
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.c29 Dec 2015 19:44:32 -  1.58
+++ keymap.c19 Mar 2021 06:02:05 -
@@ -129,7 +129,8 @@ static PF cXcB[] = {
ctrlg   /* ^G */
 };

-static PF cXcL[] = {
+static PF cXcJ[] = {
+   dired_jump, /* ^J */
lowerregion,/* ^L */
rescan, /* ^M */
rescan, /* ^N */
@@ -198,7 +199,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
Index: mg.1
===
RCS file: /cvs/src/usr.bin/mg/mg.1,v
retrieving revision 1.120
diff -u -p -r1.120 mg.1
--- mg.123 Feb 2021 18:45:33 -  1.120
+++ mg.119 Mar 2021 06:02:05 -
@@ -198,6 +198,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
@@ -523,6 +525,8 @@ Display the 

Re: mg(1): dired-jump

2021-03-18 Thread George Koehler
On Thu, 18 Mar 2021 20:22:44 + (UTC)
Mark Lumsden  wrote:

> This diff was first seen on the tech list just under a year ago. It is 
> from Philip K. and was tested by George Koehler. I have modified it 
> slightly. From Philips original post:

You sent, or I received, your diff with wrong whitespace; I needed
$ sed 's/^  / /' your.eml | patch -p0

One question below.

> 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 files line.
> 
> 
> I find this useful. ok?
> 
> Index: def.h
> ===
> RCS file: /cvs/src/usr.bin/mg/def.h,v
> retrieving revision 1.168
> diff -u -p -r1.168 def.h
> --- def.h 1 Mar 2021 10:51:14 -   1.168
> +++ def.h 18 Mar 2021 20:15:34 -
> @@ -363,6 +363,7 @@ intask_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.98
> diff -u -p -r1.98 dired.c
> --- dired.c   5 Mar 2021 16:16:53 -   1.98
> +++ dired.c   18 Mar 2021 20:15:34 -
> @@ -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 intgotofile(char*);
>   static struct buffer*refreshbuffer(struct buffer *);
>   static int   createlist(struct buffer *);
>   static void  redelete(struct buffer *);
> @@ -1091,13 +1092,38 @@ createlist(struct buffer *bp)
>   }
> 
>   int
> +dired_jump(int f, int n)
> +{
> + char dname[NFILEN], *fname;
> + struct buffer   *bp;
> + int  ret;
> +
> + 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 (strlen(fname))
> + gotofile(fname);

Should this be "if (fname != NULL)"?  Some other callers of
adjustname() check for a NULL pointer.  strlen(NULL) would crash by
SIGSEGV; strlen("") would return 0, because "" is a non-NULL pointer
to an ASCII NUL '\0'.

The old diff, that I had been running, called basename() here.  You
changed it to call adjustname().  That's better.  I didn't like
basename() because it didn't use the xbasename() wrapper.

--George

> +
> + 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 +1140,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++;
> Index: funmap.c
> ===
> RCS file: /cvs/src/usr.bin/mg/funmap.c,v
> retrieving revision 1.59
> diff -u -p -r1.59 funmap.c
> --- funmap.c  11 Jul 2019 18:20:18 -  1.59
> +++ funmap.c  18 Mar 2021 20:15:34 -
> @@ -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", 0},
>   {showversion, "emacs-version", 0},
> 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  18 Mar 2021 20:15:34 -
> @@ -129,7 +129,8 @@ static PF cXcB[] = {
>   ctrlg   /* ^G