unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* rename buffer but overwrite already existing one
@ 2020-03-23 18:05 Uwe Brauer
  2020-03-23 18:23 ` Stefan Monnier
  2020-03-23 19:18 ` Eli Zaretskii
  0 siblings, 2 replies; 13+ messages in thread
From: Uwe Brauer @ 2020-03-23 18:05 UTC (permalink / raw)
  To: emacs-devel


Hi 

Not sure my first posting arrived, if yes my apologies.
I use rename-buffer quite often, especially for message buffers.
However once a name is taken, the function refuses to overwrite a
buffer. I could of course use rename-uniquely in which case a number is
attached, but this I find quickly confusing.

Couldn't rename-buffer, optionally, overwrite the content of a buffer.

I looked around but can't find such a functionality.

Thanks and regards

Uwe Brauer 




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 18:05 rename buffer but overwrite already existing one Uwe Brauer
@ 2020-03-23 18:23 ` Stefan Monnier
  2020-03-23 19:24   ` Uwe Brauer
  2020-03-23 19:18 ` Eli Zaretskii
  1 sibling, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2020-03-23 18:23 UTC (permalink / raw)
  To: emacs-devel

> Couldn't rename-buffer, optionally, overwrite the content of a buffer.

Yes and no: it depends on what you mean by "overwrite the content of a buffer".

Here's what I think could be done easily:

- say you have buffers "mybuf" "otherbuf"
- you're in "mybuf" and you do `M-x rename-buffer RET otherbuf RET`.
- Emacs currently refuses it but it could accept your command by
  first renaming "otherbuf" to "otherbuf<1>", or by killing "otherbuf",
  or by asking you for a new name to use for that other buffer.

But the two buffers can be unified into a single one.

Of course, another option might be to tweak `rename-uniquely` so it
automatically chooses a more informative name than what you get by
adding a "<N>".  I think such a feature might be a good idea
in general.  E.g. we could add a variable `buffer-name-suffix-function`
which some major modes could set to provide "meaningful" unique names.




        Stefan




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 18:05 rename buffer but overwrite already existing one Uwe Brauer
  2020-03-23 18:23 ` Stefan Monnier
@ 2020-03-23 19:18 ` Eli Zaretskii
  2020-03-23 19:23   ` Uwe Brauer
  1 sibling, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2020-03-23 19:18 UTC (permalink / raw)
  To: Uwe Brauer; +Cc: emacs-devel

> From: Uwe Brauer <oub@mat.ucm.es>
> Date: Mon, 23 Mar 2020 19:05:23 +0100
> 
> Couldn't rename-buffer, optionally, overwrite the content of a buffer.

Yes, but it's called copy-to-buffer, not rename-buffer.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 19:18 ` Eli Zaretskii
@ 2020-03-23 19:23   ` Uwe Brauer
  2020-03-23 19:35     ` Eli Zaretskii
  2020-03-23 19:41     ` Andreas Schwab
  0 siblings, 2 replies; 13+ messages in thread
From: Uwe Brauer @ 2020-03-23 19:23 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 500 bytes --]

>>> "EZ" == Eli Zaretskii <eliz@gnu.org> writes:

   >> From: Uwe Brauer <oub@mat.ucm.es>
   >> Date: Mon, 23 Mar 2020 19:05:23 +0100
   >> 
   >> Couldn't rename-buffer, optionally, overwrite the content of a buffer.

   > Yes, but it's called copy-to-buffer, not rename-buffer.

Thanks for pointing this out to me, but I have to mark the whole buffer
first, which is a but inconvenient, I say.

Couldn't there an argument added so that with the argument the whole
buffer is copied?


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 18:23 ` Stefan Monnier
@ 2020-03-23 19:24   ` Uwe Brauer
  0 siblings, 0 replies; 13+ messages in thread
From: Uwe Brauer @ 2020-03-23 19:24 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1232 bytes --]

>>> "SM" == Stefan Monnier <monnier@iro.umontreal.ca> writes:

   >> Couldn't rename-buffer, optionally, overwrite the content of a buffer.
   > Yes and no: it depends on what you mean by "overwrite the content of a buffer".

   > Here's what I think could be done easily:

   > - say you have buffers "mybuf" "otherbuf"
   > - you're in "mybuf" and you do `M-x rename-buffer RET otherbuf RET`.
   > - Emacs currently refuses it but it could accept your command by
   >   first renaming "otherbuf" to "otherbuf<1>", or by 
   >   killing "otherbuf",
^^^^^^^^^^^^^^^^^^^^^^^^^^
this is what I would prefer! Kill it.

   >   or by asking you for a new name to use for that other buffer.

   > But the two buffers can be unified into a single one.

   > Of course, another option might be to tweak `rename-uniquely` so it
   > automatically chooses a more informative name than what you get by
   > adding a "<N>".  I think such a feature might be a good idea
   > in general.  E.g. we could add a variable `buffer-name-suffix-function`
   > which some major modes could set to provide "meaningful" unique names.

Yeah, but I tend to loose the track easily of these buffers. 



   >         Stefan




[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 19:23   ` Uwe Brauer
@ 2020-03-23 19:35     ` Eli Zaretskii
  2020-03-23 19:46       ` Uwe Brauer
  2020-03-23 19:41     ` Andreas Schwab
  1 sibling, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2020-03-23 19:35 UTC (permalink / raw)
  To: Uwe Brauer; +Cc: emacs-devel

> From: Uwe Brauer <oub@mat.ucm.es>
> Date: Mon, 23 Mar 2020 20:23:15 +0100
> 
>    >> Couldn't rename-buffer, optionally, overwrite the content of a buffer.
> 
>    > Yes, but it's called copy-to-buffer, not rename-buffer.
> 
> Thanks for pointing this out to me, but I have to mark the whole buffer
> first, which is a but inconvenient, I say.

That's just "C-x h" away.

> Couldn't there an argument added so that with the argument the whole
> buffer is copied?

I suggest to submit a feature request.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 19:23   ` Uwe Brauer
  2020-03-23 19:35     ` Eli Zaretskii
@ 2020-03-23 19:41     ` Andreas Schwab
  2020-03-23 19:43       ` Uwe Brauer
  1 sibling, 1 reply; 13+ messages in thread
From: Andreas Schwab @ 2020-03-23 19:41 UTC (permalink / raw)
  To: emacs-devel

On Mär 23 2020, Uwe Brauer wrote:

>>>> "EZ" == Eli Zaretskii <eliz@gnu.org> writes:
>
>    >> From: Uwe Brauer <oub@mat.ucm.es>
>    >> Date: Mon, 23 Mar 2020 19:05:23 +0100
>    >> 
>    >> Couldn't rename-buffer, optionally, overwrite the content of a buffer.
>
>    > Yes, but it's called copy-to-buffer, not rename-buffer.
>
> Thanks for pointing this out to me, but I have to mark the whole buffer
> first, which is a but inconvenient, I say.
>
> Couldn't there an argument added so that with the argument the whole
> buffer is copied?

It's easy to define your own function that calls copy-to-buffer with
(point-min), (point-max).

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 19:41     ` Andreas Schwab
@ 2020-03-23 19:43       ` Uwe Brauer
  2020-03-23 20:02         ` Stephen Berman
  0 siblings, 1 reply; 13+ messages in thread
From: Uwe Brauer @ 2020-03-23 19:43 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1241 bytes --]

>>> "AS" == Andreas Schwab <schwab@linux-m68k.org> writes:

   > On Mär 23 2020, Uwe Brauer wrote:
   >>>>> "EZ" == Eli Zaretskii <eliz@gnu.org> writes:
   >> 
   >> >> From: Uwe Brauer <oub@mat.ucm.es>
   >> >> Date: Mon, 23 Mar 2020 19:05:23 +0100
   >> >> 
   >> >> Couldn't rename-buffer, optionally, overwrite the content of a buffer.
   >> 
   >> > Yes, but it's called copy-to-buffer, not rename-buffer.
   >> 
   >> Thanks for pointing this out to me, but I have to mark the whole buffer
   >> first, which is a but inconvenient, I say.
   >> 
   >> Couldn't there an argument added so that with the argument the whole
   >> buffer is copied?

   > It's easy to define your own function that calls copy-to-buffer with
   > (point-min), (point-max).

That is what I thought

(defun my-copy-to-buffer (buffer)
  "Copy to specified BUFFER the whole buffer."
  (interactive "BCopy to buffer: \n")
  (let ((oldbuf (current-buffer)))
    (with-current-buffer (get-buffer-create buffer)
      (barf-if-buffer-read-only)
      (erase-buffer)
      (save-excursion
	(insert-buffer-substring oldbuf (point-min) (point-max))))))


Does not work zero bytes where copied, there is something I miss, obviously.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 19:35     ` Eli Zaretskii
@ 2020-03-23 19:46       ` Uwe Brauer
  0 siblings, 0 replies; 13+ messages in thread
From: Uwe Brauer @ 2020-03-23 19:46 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1132 bytes --]

>>> "EZ" == Eli Zaretskii <eliz@gnu.org> writes:

   >> From: Uwe Brauer <oub@mat.ucm.es>
   >> Date: Mon, 23 Mar 2020 20:23:15 +0100
   >> 
   >> >> Couldn't rename-buffer, optionally, overwrite the content of a buffer.
   >> 
   >> > Yes, but it's called copy-to-buffer, not rename-buffer.
   >> 
   >> Thanks for pointing this out to me, but I have to mark the whole buffer
   >> first, which is a but inconvenient, I say.

   > That's just "C-x h" away.

I know, but decades of abusing rename-buffer are difficult to come by..


   >> Couldn't there an argument added so that with the argument the whole
   >> buffer is copied?

   > I suggest to submit a feature request.

I thought that even I could implement this
but


(defun my-copy-to-buffer (buffer)
  "Copy to specified BUFFER the whole buffer."
  (interactive "BCopy to buffer: \n")
  (let ((oldbuf (current-buffer)))
    (with-current-buffer (get-buffer-create buffer)
      (barf-if-buffer-read-only)
      (erase-buffer)
      (save-excursion
	(insert-buffer-substring oldbuf (point-min) (point-max))))))

Copies 0 bytes. sigh


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 19:43       ` Uwe Brauer
@ 2020-03-23 20:02         ` Stephen Berman
  2020-03-23 20:49           ` Uwe Brauer
  0 siblings, 1 reply; 13+ messages in thread
From: Stephen Berman @ 2020-03-23 20:02 UTC (permalink / raw)
  To: emacs-devel

On Mon, 23 Mar 2020 20:43:56 +0100 Uwe Brauer <oub@mat.ucm.es> wrote:

>>>> "AS" == Andreas Schwab <schwab@linux-m68k.org> writes:
>
>    > On Mär 23 2020, Uwe Brauer wrote:
>    >>>>> "EZ" == Eli Zaretskii <eliz@gnu.org> writes:
>    >> 
>    >> >> From: Uwe Brauer <oub@mat.ucm.es>
>    >> >> Date: Mon, 23 Mar 2020 19:05:23 +0100
>    >> >> 
>    >> >> Couldn't rename-buffer, optionally, overwrite the content of a buffer.
>    >> 
>    >> > Yes, but it's called copy-to-buffer, not rename-buffer.
>    >> 
>    >> Thanks for pointing this out to me, but I have to mark the whole buffer
>    >> first, which is a but inconvenient, I say.
>    >> 
>    >> Couldn't there an argument added so that with the argument the whole
>    >> buffer is copied?
>
>    > It's easy to define your own function that calls copy-to-buffer with
>    > (point-min), (point-max).
>
> That is what I thought
>
> (defun my-copy-to-buffer (buffer)
>   "Copy to specified BUFFER the whole buffer."
>   (interactive "BCopy to buffer: \n")
>   (let ((oldbuf (current-buffer)))
>     (with-current-buffer (get-buffer-create buffer)
>       (barf-if-buffer-read-only)
>       (erase-buffer)
>       (save-excursion
> 	(insert-buffer-substring oldbuf (point-min) (point-max))))))
>
>
> Does not work zero bytes where copied, there is something I miss, obviously.

You're calling insert-buffer-substring with point-min and point-max of
the buffer you've just created, so they're both 1.  Try this:

(defun my-copy-to-buffer (buffer)
  "Copy the contents of the current buffer to  BUFFER."
  (interactive "BCopy to buffer: ")
  (let ((start (point-min))
	(end (point-max)))
    (copy-to-buffer buffer start end)))

Steve Berman



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 20:02         ` Stephen Berman
@ 2020-03-23 20:49           ` Uwe Brauer
  2020-03-23 21:21             ` Stephen Berman
  0 siblings, 1 reply; 13+ messages in thread
From: Uwe Brauer @ 2020-03-23 20:49 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 992 bytes --]


   > On Mon, 23 Mar 2020 20:43:56 +0100 Uwe Brauer <oub@mat.ucm.es> wrote:

   > You're calling insert-buffer-substring with point-min and point-max of
   > the buffer you've just created, so they're both 1.  Try this:

   > (defun my-copy-to-buffer (buffer)
   >   "Copy the contents of the current buffer to  BUFFER."
   >   (interactive "BCopy to buffer: ")
   >   (let ((start (point-min))
   > 	(end (point-max)))
   >     (copy-to-buffer buffer start end)))

Thanks I tried this 


(defun my-copy-to-buffer (buffer)
  "Copy to specified BUFFER the whole buffer."
  (interactive "BCopy to buffer: \n")
  (let ((oldbuf (current-buffer))
        (start (point-min))
        (end (point-max)))
    
    (with-current-buffer (get-buffer-create buffer)
      (barf-if-buffer-read-only)
      (save-excursion
	(insert-buffer-substring oldbuf (point-min) (point-max))))))

And it did not work,

Your code works though.


I will never understand let


Uwe 

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 20:49           ` Uwe Brauer
@ 2020-03-23 21:21             ` Stephen Berman
  2020-03-24  9:17               ` Uwe Brauer
  0 siblings, 1 reply; 13+ messages in thread
From: Stephen Berman @ 2020-03-23 21:21 UTC (permalink / raw)
  To: emacs-devel

On Mon, 23 Mar 2020 21:49:57 +0100 Uwe Brauer <oub@mat.ucm.es> wrote:

>    > On Mon, 23 Mar 2020 20:43:56 +0100 Uwe Brauer <oub@mat.ucm.es> wrote:
>
>    > You're calling insert-buffer-substring with point-min and point-max of
>    > the buffer you've just created, so they're both 1.  Try this:
>
>    > (defun my-copy-to-buffer (buffer)
>    >   "Copy the contents of the current buffer to  BUFFER."
>    >   (interactive "BCopy to buffer: ")
>    >   (let ((start (point-min))
>    > 	(end (point-max)))
>    >     (copy-to-buffer buffer start end)))
>
> Thanks I tried this
>
>
> (defun my-copy-to-buffer (buffer)
>   "Copy to specified BUFFER the whole buffer."
>   (interactive "BCopy to buffer: \n")
>   (let ((oldbuf (current-buffer))
>         (start (point-min))
>         (end (point-max)))
>
>     (with-current-buffer (get-buffer-create buffer)
>       (barf-if-buffer-read-only)
>       (save-excursion
> 	(insert-buffer-substring oldbuf (point-min) (point-max))))))
>
> And it did not work,

This function doesn't use the local variables `start' and `end', and
evaluating (point-min) and (point-max) in the scope of
with-current-buffer again returns the values for the buffer just
created.  Replacing `(point-min)' and `(point-max)' there by `start' and
`end', respectively, will DTRT, but then you can just use copy-to-buffer
as I showed above.

Steve Berman



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: rename buffer but overwrite already existing one
  2020-03-23 21:21             ` Stephen Berman
@ 2020-03-24  9:17               ` Uwe Brauer
  0 siblings, 0 replies; 13+ messages in thread
From: Uwe Brauer @ 2020-03-24  9:17 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 540 bytes --]


   > On Mon, 23 Mar 2020 21:49:57 +0100 Uwe Brauer <oub@mat.ucm.es> wrote:

   > This function doesn't use the local variables `start' and `end', and
   > evaluating (point-min) and (point-max) in the scope of
   > with-current-buffer again returns the values for the buffer just
   > created.  Replacing `(point-min)' and `(point-max)' there by `start' and
   > `end', respectively, will DTRT, but then you can just use copy-to-buffer

Ahh, I forgot to modify the last line :-[

Thanks for your help. I will try to add an argument later.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-03-24  9:17 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-23 18:05 rename buffer but overwrite already existing one Uwe Brauer
2020-03-23 18:23 ` Stefan Monnier
2020-03-23 19:24   ` Uwe Brauer
2020-03-23 19:18 ` Eli Zaretskii
2020-03-23 19:23   ` Uwe Brauer
2020-03-23 19:35     ` Eli Zaretskii
2020-03-23 19:46       ` Uwe Brauer
2020-03-23 19:41     ` Andreas Schwab
2020-03-23 19:43       ` Uwe Brauer
2020-03-23 20:02         ` Stephen Berman
2020-03-23 20:49           ` Uwe Brauer
2020-03-23 21:21             ` Stephen Berman
2020-03-24  9:17               ` Uwe Brauer

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).