mg: revert-buffer

2012-10-12 Thread Jasper Lievisse Adriaanse
Hi,

Here's a diff that implement revert-buffer (C-x r). I've split gotoline into
the 'goto-line' specifics and the code that actually jumps to the line so it
can be re-used by revert-buffer to restore the current line.

OK?

-- 
Cheers,
Jasper

Stay Hungry. Stay Foolish

Index: basic.c
===
RCS file: /cvs/src/usr.bin/mg/basic.c,v
retrieving revision 1.37
diff -p -u -r1.37 basic.c
--- basic.c 18 Jun 2012 09:26:03 -  1.37
+++ basic.c 12 Oct 2012 10:25:14 -
@@ -485,7 +485,6 @@ swapmark(int f, int n)
 int
 gotoline(int f, int n)
 {
-   struct line  *clp;
char   buf[32], *bufp;
const char *err;
 
@@ -501,6 +500,16 @@ gotoline(int f, int n)
return (FALSE);
}
}
+   return(setlineno(n));
+}
+
+/*
+ * Set the line number and switch to it.
+ */
+int
+setlineno(int n){
+   struct line  *clp;
+
if (n = 0) {
if (n == 0)
n++;
Index: buffer.c
===
RCS file: /cvs/src/usr.bin/mg/buffer.c,v
retrieving revision 1.81
diff -p -u -r1.81 buffer.c
--- buffer.c31 Aug 2012 18:06:42 -  1.81
+++ buffer.c12 Oct 2012 10:25:14 -
@@ -861,4 +861,35 @@ checkdirty(struct buffer *bp)
 
return (TRUE);
 }
-   
+
+/*
+ * Revert the current buffer to whatever is on disk.
+ */
+/* ARGSUSED */
+int
+revertbuffer(int f, int n){
+   struct mgwin *wp = wheadp;
+   struct buffer *bp = wp-w_bufp;
+   char fbuf[NFILEN + 32];
+   int lineno;
+
+   if (!strlen(bp-b_fname)) {
+   ewprintf(Cannot revert buffer not associated with any files.);
+   return (FALSE);
+   }
+
+   snprintf(fbuf, sizeof(fbuf), Revert buffer from file %s, bp-b_fname);
+
+   if (eyorn(fbuf)) {
+   /* Save our current line, so we can go back it after reloading 
the file. */
+   lineno = wp-w_dotline;
+   if (readin(bp-b_list.l_name)) {
+   return(setlineno(lineno));
+   } else {
+   ewprintf(File %s no longer readable!, bp-b_fname);
+   return (FALSE);
+   }
+   }
+
+   return (FALSE);
+}
Index: def.h
===
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.125
diff -p -u -r1.125 def.h
--- def.h   31 Aug 2012 18:06:42 -  1.125
+++ def.h   12 Oct 2012 10:25:14 -
@@ -414,6 +414,7 @@ int  notmodified(int, int);
 int popbuftop(struct buffer *, int);
 int getbufcwd(char *, size_t);
 int checkdirty(struct buffer *);
+int revertbuffer(int, int);
 
 /* display.c */
 intvtresize(int, int, int);
@@ -494,6 +495,7 @@ int  setmark(int, int);
 int clearmark(int, int);
 int swapmark(int, int);
 int gotoline(int, int);
+int setlineno(int);
 
 /* random.c X */
 int showcpos(int, int);
Index: funmap.c
===
RCS file: /cvs/src/usr.bin/mg/funmap.c,v
retrieving revision 1.40
diff -p -u -r1.40 funmap.c
--- funmap.c14 Jun 2012 17:21:22 -  1.40
+++ funmap.c12 Oct 2012 10:25:15 -
@@ -196,7 +196,8 @@ static struct funmap functnames[] = {
{csprevmatch, cscope-prev-symbol,},
{csnextfile, cscope-next-file,},
{csprevfile, cscope-prev-file,},
-   {cscreatelist, cscope-create-list-of-files-to-index},
+   {cscreatelist, cscope-create-list-of-files-to-index,},
+   {revertbuffer, revert-buffer,},
{NULL, NULL,}
 };
 
Index: keymap.c
===
RCS file: /cvs/src/usr.bin/mg/keymap.c,v
retrieving revision 1.50
diff -p -u -r1.50 keymap.c
--- keymap.c7 Jun 2012 15:15:04 -   1.50
+++ keymap.c12 Oct 2012 10:25:15 -
@@ -177,7 +177,7 @@ static PF cXcar[] = {
nextwind,   /* o */
prevwind,   /* p */
rescan, /* q */
-   rescan, /* r */
+   revertbuffer,   /* r */
savebuffers,/* s */
rescan, /* t */
undo/* u */
Index: mg.1
===
RCS file: /cvs/src/usr.bin/mg/mg.1,v
retrieving revision 1.68
diff -p -u -r1.68 mg.1
--- mg.111 Jul 2012 19:56:13 -  1.68
+++ mg.112 Oct 2012 10:25:15 -
@@ -249,6 +249,8 @@ other-window
 other-window
 .It C-x p
 previous-window
+.It C-x r
+revert-buffer
 .It C-x s
 save-some-buffers
 .It C-x u
@@ -752,6 +754,8 @@ is negative, it is that line from the bo
 .It redraw-display
 Refresh the display.
 Recomputes all window sizes in case something has changed.

Re: mg: revert-buffer

2012-10-12 Thread Christiano F. Haesbaert
I want this baadlyyy, ill have a look.
On Oct 12, 2012 12:30 PM, Jasper Lievisse Adriaanse jas...@openbsd.org
wrote:

 Hi,

 Here's a diff that implement revert-buffer (C-x r). I've split gotoline
 into
 the 'goto-line' specifics and the code that actually jumps to the line so
 it
 can be re-used by revert-buffer to restore the current line.

 OK?

 --
 Cheers,
 Jasper

 Stay Hungry. Stay Foolish

 Index: basic.c
 ===
 RCS file: /cvs/src/usr.bin/mg/basic.c,v
 retrieving revision 1.37
 diff -p -u -r1.37 basic.c
 --- basic.c 18 Jun 2012 09:26:03 -  1.37
 +++ basic.c 12 Oct 2012 10:25:14 -
 @@ -485,7 +485,6 @@ swapmark(int f, int n)
  int
  gotoline(int f, int n)
  {
 -   struct line  *clp;
 char   buf[32], *bufp;
 const char *err;

 @@ -501,6 +500,16 @@ gotoline(int f, int n)
 return (FALSE);
 }
 }
 +   return(setlineno(n));
 +}
 +
 +/*
 + * Set the line number and switch to it.
 + */
 +int
 +setlineno(int n){
 +   struct line  *clp;
 +
 if (n = 0) {
 if (n == 0)
 n++;
 Index: buffer.c
 ===
 RCS file: /cvs/src/usr.bin/mg/buffer.c,v
 retrieving revision 1.81
 diff -p -u -r1.81 buffer.c
 --- buffer.c31 Aug 2012 18:06:42 -  1.81
 +++ buffer.c12 Oct 2012 10:25:14 -
 @@ -861,4 +861,35 @@ checkdirty(struct buffer *bp)

 return (TRUE);
  }
 -
 +
 +/*
 + * Revert the current buffer to whatever is on disk.
 + */
 +/* ARGSUSED */
 +int
 +revertbuffer(int f, int n){
 +   struct mgwin *wp = wheadp;
 +   struct buffer *bp = wp-w_bufp;
 +   char fbuf[NFILEN + 32];
 +   int lineno;
 +
 +   if (!strlen(bp-b_fname)) {
 +   ewprintf(Cannot revert buffer not associated with any
 files.);
 +   return (FALSE);
 +   }
 +
 +   snprintf(fbuf, sizeof(fbuf), Revert buffer from file %s,
 bp-b_fname);
 +
 +   if (eyorn(fbuf)) {
 +   /* Save our current line, so we can go back it after
 reloading the file. */
 +   lineno = wp-w_dotline;
 +   if (readin(bp-b_list.l_name)) {
 +   return(setlineno(lineno));
 +   } else {
 +   ewprintf(File %s no longer readable!,
 bp-b_fname);
 +   return (FALSE);
 +   }
 +   }
 +
 +   return (FALSE);
 +}
 Index: def.h
 ===
 RCS file: /cvs/src/usr.bin/mg/def.h,v
 retrieving revision 1.125
 diff -p -u -r1.125 def.h
 --- def.h   31 Aug 2012 18:06:42 -  1.125
 +++ def.h   12 Oct 2012 10:25:14 -
 @@ -414,6 +414,7 @@ int  notmodified(int, int);
  int popbuftop(struct buffer *, int);
  int getbufcwd(char *, size_t);
  int checkdirty(struct buffer *);
 +int revertbuffer(int, int);

  /* display.c */
  intvtresize(int, int, int);
 @@ -494,6 +495,7 @@ int  setmark(int, int);
  int clearmark(int, int);
  int swapmark(int, int);
  int gotoline(int, int);
 +int setlineno(int);

  /* random.c X */
  int showcpos(int, int);
 Index: funmap.c
 ===
 RCS file: /cvs/src/usr.bin/mg/funmap.c,v
 retrieving revision 1.40
 diff -p -u -r1.40 funmap.c
 --- funmap.c14 Jun 2012 17:21:22 -  1.40
 +++ funmap.c12 Oct 2012 10:25:15 -
 @@ -196,7 +196,8 @@ static struct funmap functnames[] = {
 {csprevmatch, cscope-prev-symbol,},
 {csnextfile, cscope-next-file,},
 {csprevfile, cscope-prev-file,},
 -   {cscreatelist, cscope-create-list-of-files-to-index},
 +   {cscreatelist, cscope-create-list-of-files-to-index,},
 +   {revertbuffer, revert-buffer,},
 {NULL, NULL,}
  };

 Index: keymap.c
 ===
 RCS file: /cvs/src/usr.bin/mg/keymap.c,v
 retrieving revision 1.50
 diff -p -u -r1.50 keymap.c
 --- keymap.c7 Jun 2012 15:15:04 -   1.50
 +++ keymap.c12 Oct 2012 10:25:15 -
 @@ -177,7 +177,7 @@ static PF cXcar[] = {
 nextwind,   /* o */
 prevwind,   /* p */
 rescan, /* q */
 -   rescan, /* r */
 +   revertbuffer,   /* r */
 savebuffers,/* s */
 rescan, /* t */
 undo/* u */
 Index: mg.1
 ===
 RCS file: /cvs/src/usr.bin/mg/mg.1,v
 retrieving revision 1.68
 diff -p -u -r1.68 mg.1
 --- mg.111 Jul 2012 19:56:13 -  1.68
 +++ mg.112 Oct 2012 10:25:15 -
 @@ -249,6 +249,8 @@ other-window
  other-window

Re: mg: revert-buffer

2012-10-12 Thread Jasper Lievisse Adriaanse
Seems I broke something between testing and sending out the diff. Updating
diff coming later.

On Fri, Oct 12, 2012 at 12:40:47PM +0200, Christiano F. Haesbaert wrote:
 I want this baadlyyy, ill have a look.
 On Oct 12, 2012 12:30 PM, Jasper Lievisse Adriaanse jas...@openbsd.org
 wrote:
 
  Hi,
 
  Here's a diff that implement revert-buffer (C-x r). I've split gotoline
  into
  the 'goto-line' specifics and the code that actually jumps to the line so
  it
  can be re-used by revert-buffer to restore the current line.
 
  OK?
 
  --
  Cheers,
  Jasper
 
  Stay Hungry. Stay Foolish
 
  Index: basic.c
  ===
  RCS file: /cvs/src/usr.bin/mg/basic.c,v
  retrieving revision 1.37
  diff -p -u -r1.37 basic.c
  --- basic.c 18 Jun 2012 09:26:03 -  1.37
  +++ basic.c 12 Oct 2012 10:25:14 -
  @@ -485,7 +485,6 @@ swapmark(int f, int n)
   int
   gotoline(int f, int n)
   {
  -   struct line  *clp;
  char   buf[32], *bufp;
  const char *err;
 
  @@ -501,6 +500,16 @@ gotoline(int f, int n)
  return (FALSE);
  }
  }
  +   return(setlineno(n));
  +}
  +
  +/*
  + * Set the line number and switch to it.
  + */
  +int
  +setlineno(int n){
  +   struct line  *clp;
  +
  if (n = 0) {
  if (n == 0)
  n++;
  Index: buffer.c
  ===
  RCS file: /cvs/src/usr.bin/mg/buffer.c,v
  retrieving revision 1.81
  diff -p -u -r1.81 buffer.c
  --- buffer.c31 Aug 2012 18:06:42 -  1.81
  +++ buffer.c12 Oct 2012 10:25:14 -
  @@ -861,4 +861,35 @@ checkdirty(struct buffer *bp)
 
  return (TRUE);
   }
  -
  +
  +/*
  + * Revert the current buffer to whatever is on disk.
  + */
  +/* ARGSUSED */
  +int
  +revertbuffer(int f, int n){
  +   struct mgwin *wp = wheadp;
  +   struct buffer *bp = wp-w_bufp;
  +   char fbuf[NFILEN + 32];
  +   int lineno;
  +
  +   if (!strlen(bp-b_fname)) {
  +   ewprintf(Cannot revert buffer not associated with any
  files.);
  +   return (FALSE);
  +   }
  +
  +   snprintf(fbuf, sizeof(fbuf), Revert buffer from file %s,
  bp-b_fname);
  +
  +   if (eyorn(fbuf)) {
  +   /* Save our current line, so we can go back it after
  reloading the file. */
  +   lineno = wp-w_dotline;
  +   if (readin(bp-b_list.l_name)) {
  +   return(setlineno(lineno));
  +   } else {
  +   ewprintf(File %s no longer readable!,
  bp-b_fname);
  +   return (FALSE);
  +   }
  +   }
  +
  +   return (FALSE);
  +}
  Index: def.h
  ===
  RCS file: /cvs/src/usr.bin/mg/def.h,v
  retrieving revision 1.125
  diff -p -u -r1.125 def.h
  --- def.h   31 Aug 2012 18:06:42 -  1.125
  +++ def.h   12 Oct 2012 10:25:14 -
  @@ -414,6 +414,7 @@ int  notmodified(int, int);
   int popbuftop(struct buffer *, int);
   int getbufcwd(char *, size_t);
   int checkdirty(struct buffer *);
  +int revertbuffer(int, int);
 
   /* display.c */
   intvtresize(int, int, int);
  @@ -494,6 +495,7 @@ int  setmark(int, int);
   int clearmark(int, int);
   int swapmark(int, int);
   int gotoline(int, int);
  +int setlineno(int);
 
   /* random.c X */
   int showcpos(int, int);
  Index: funmap.c
  ===
  RCS file: /cvs/src/usr.bin/mg/funmap.c,v
  retrieving revision 1.40
  diff -p -u -r1.40 funmap.c
  --- funmap.c14 Jun 2012 17:21:22 -  1.40
  +++ funmap.c12 Oct 2012 10:25:15 -
  @@ -196,7 +196,8 @@ static struct funmap functnames[] = {
  {csprevmatch, cscope-prev-symbol,},
  {csnextfile, cscope-next-file,},
  {csprevfile, cscope-prev-file,},
  -   {cscreatelist, cscope-create-list-of-files-to-index},
  +   {cscreatelist, cscope-create-list-of-files-to-index,},
  +   {revertbuffer, revert-buffer,},
  {NULL, NULL,}
   };
 
  Index: keymap.c
  ===
  RCS file: /cvs/src/usr.bin/mg/keymap.c,v
  retrieving revision 1.50
  diff -p -u -r1.50 keymap.c
  --- keymap.c7 Jun 2012 15:15:04 -   1.50
  +++ keymap.c12 Oct 2012 10:25:15 -
  @@ -177,7 +177,7 @@ static PF cXcar[] = {
  nextwind,   /* o */
  prevwind,   /* p */
  rescan, /* q */
  -   rescan, /* r */
  +   revertbuffer,   /* r */
  savebuffers,/* s */
  rescan, /* t */
  undo

Re: mg: revert-buffer

2012-10-12 Thread Florian Obser

On 10/12/2012 02:49 PM, Jasper Lievisse Adriaanse wrote:

Here's an updated diff that doesn't mix buffername with filenames.

Index: basic.c
===
RCS file: /cvs/src/usr.bin/mg/basic.c,v
retrieving revision 1.37
diff -p -u -r1.37 basic.c
--- basic.c 18 Jun 2012 09:26:03 -  1.37
+++ basic.c 12 Oct 2012 12:49:21 -
@@ -485,7 +485,6 @@ swapmark(int f, int n)
  int
  gotoline(int f, int n)
  {
-   struct line  *clp;
char   buf[32], *bufp;
const char *err;

@@ -501,6 +500,16 @@ gotoline(int f, int n)
return (FALSE);
}
}
+   return(setlineno(n));
+}
+
+/*
+ * Set the line number and switch to it.
+ */
+int
+setlineno(int n){
+   struct line  *clp;
+
if (n= 0) {
if (n == 0)
n++;
Index: buffer.c
===
RCS file: /cvs/src/usr.bin/mg/buffer.c,v
retrieving revision 1.81
diff -p -u -r1.81 buffer.c
--- buffer.c31 Aug 2012 18:06:42 -  1.81
+++ buffer.c12 Oct 2012 12:49:21 -
@@ -861,4 +861,35 @@ checkdirty(struct buffer *bp)

return (TRUE);
  }
-   
+
+/*
+ * Revert the current buffer to whatever is on disk.
+ */
+/* ARGSUSED */
+int
+revertbuffer(int f, int n){
+   struct mgwin *wp = wheadp;
+   struct buffer *bp = wp-w_bufp;
+   char fbuf[NFILEN + 32];
+   int lineno;
+
+   if (!strlen(bp-b_fname)) {
+   ewprintf(Cannot revert buffer not associated with any files.);
+   return (FALSE);
+   }
+
+   snprintf(fbuf, sizeof(fbuf), Revert buffer from file %s, bp-b_fname);
+
+   if (eyorn(fbuf)) {
+   /* Save our current line, so we can go back it after reloading 
the file. */
+   lineno = wp-w_dotline;
+   if (readin(bp-b_fname)) {


mg asks two times for confirmation when reverting a changed buffer. I 
find that annoying and emacs doesn't do this. We could pretend the 
buffer is unchanged by

curbp-b_flag = ~BFCHG;
before readin (and keep a copy of the BFCHG flag for the error path, but 
see below).



+   return(setlineno(lineno));
+   } else {
+   ewprintf(File %s no longer readable!, bp-b_fname);
+   return (FALSE);


The error path is weird.
1) When moving the underlying file out of the way and reverting the 
buffer you get an empty buffer and mg says (New file). When you change 
the buffer and try to save it you get

Backup error, save anyway? (yes or no)
2) When the underlying file is chmod 000 mg says File no longer 
readable but the buffer is cleared. The window is not repainted. After 
entering a character you see that the buffer is empty.


In both cases emacs shows an error message and leaves the buffer untouched.


+   }
+   }
+
+   return (FALSE);
+}
Index: def.h
===
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.125
diff -p -u -r1.125 def.h
--- def.h   31 Aug 2012 18:06:42 -  1.125
+++ def.h   12 Oct 2012 12:49:21 -
@@ -414,6 +414,7 @@ int  notmodified(int, int);
  intpopbuftop(struct buffer *, int);
  intgetbufcwd(char *, size_t);
  intcheckdirty(struct buffer *);
+int revertbuffer(int, int);

  /* display.c */
  int   vtresize(int, int, int);
@@ -494,6 +495,7 @@ int  setmark(int, int);
  intclearmark(int, int);
  intswapmark(int, int);
  intgotoline(int, int);
+int setlineno(int);

  /* random.c X */
  intshowcpos(int, int);
Index: funmap.c
===
RCS file: /cvs/src/usr.bin/mg/funmap.c,v
retrieving revision 1.40
diff -p -u -r1.40 funmap.c
--- funmap.c14 Jun 2012 17:21:22 -  1.40
+++ funmap.c12 Oct 2012 12:49:22 -
@@ -196,7 +196,8 @@ static struct funmap functnames[] = {
{csprevmatch, cscope-prev-symbol,},
{csnextfile, cscope-next-file,},
{csprevfile, cscope-prev-file,},
-   {cscreatelist, cscope-create-list-of-files-to-index},
+   {cscreatelist, cscope-create-list-of-files-to-index,},
+   {revertbuffer, revert-buffer,},
{NULL, NULL,}
  };

Index: keymap.c
===
RCS file: /cvs/src/usr.bin/mg/keymap.c,v
retrieving revision 1.50
diff -p -u -r1.50 keymap.c
--- keymap.c7 Jun 2012 15:15:04 -   1.50
+++ keymap.c12 Oct 2012 12:49:22 -
@@ -177,7 +177,7 @@ static PF cXcar[] = {
nextwind,   /* o */
prevwind,   /* p */
rescan, /* q */
-   rescan, /* r */
+   revertbuffer,   /* r */


fwiw, C-x r is a