[RFC] Do indent-region only on editable buffers

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[RFC] Do indent-region only on editable buffers

Kaushal Modi
indent-region is an act of editing a buffer. So before attempting to do that, it should be checked if the buffer is editable.

Bug#22819[1] raises that point.

The proposal is to call (barf-if-buffer-read-only) before attempting to do the indent.

Motive for this change:

The act of indenting is an editing action. So the buffer should be checked if it's editable before attempting an indent. If the buffer is read-only and no indentation change is required, then good. But what if indentation change is required? Here's what's will happen: 1. User: Try indentation (C-x h M-x indent-region) 2. User: Could take several seconds or few minutes (depending on major mode and file size) 3. Emacs: "Bummer, couldn't save all that indentation because the buffer is read-only". 4. User: Make buffer editable. It's not a simple act of chmod. In my case, the buffer was read-only because the file is part of a centralized version control system (Cliosoft SOS). In "checked in" state, the file is just a symlink to the cached version in server, and thus read-only. To make it editable, I need to "check out" the file. That act replaces the symlink link with a physical file copy. 5. User: Re-do that several seconds/minutes long indentation.

So the motive is to save time and alert user of their pilot error before attempting to do an operation that would go to waste (in this case, doing indent, and then waste all those CPU cycles because the changes couldn't get saved to the file).

Question to the list:

- Are there any objections to doing the buffer read-only check before doing the indent?

(PS: If user's still want to do indentation on read-only buffers, then can always do C-x C-q (toggle read-only-mode) and then proceed to indent the buffer.)

--

Kaushal Modi

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: [RFC] Do indent-region only on editable buffers

Drew Adams
> - Are there any objections to doing the buffer read-only check before
>   doing the indent?

Dunno what the right approach/solution is in this particular case.

But note that we don't do what you suggest in general - we don't do it
for most other editing operations: insert or delete a character, yank
a string, kill the region,...  Instead, we let an error be raised,
telling the user that the buffer is read-only.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RFC] Do indent-region only on editable buffers

Kaushal Modi
On Mon, Aug 7, 2017 at 1:47 PM Drew Adams <[hidden email]> wrote:
> - Are there any objections to doing the buffer read-only check before
>   doing the indent?

Dunno what the right approach/solution is in this particular case.

But note that we don't do what you suggest in general - we don't do it
for most other editing operations: insert or delete a character, yank
a string, kill the region,...  Instead, we let an error be raised,
telling the user that the buffer is read-only.

This case feels a bit different because the operation can be very expensive.. a few seconds or couple of minutes long indentation, based on the major mode and file size. So a user would find it tremendously helpful to know that the operation is useless on a read-only buffer before spending that amount of time.

With the cases of insert/delete char, yank string, kill region, I have never noticed any tangible delay (read: even a second).. so in those, it do not matter if you "do and then flag error" or "flag an error and then do".

Also there are many cases of commands meant for buffer editing (like keep-lines, flush-lines, etc.) where barf-if-buffer-read-only is used for the same purpose.

The purpose of this email thread is to understand if there's a workflow someone uses that would break if this read-only check were done at the beginning of the indentation operation. As Eli states his concern in the Bug#22819, do people rely on side-effects other than the actual buffer-editing indentation operation?
--

Kaushal Modi

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RFC] Do indent-region only on editable buffers

Noam Postavsky-2
In reply to this post by Drew Adams
On Mon, Aug 7, 2017 at 1:46 PM, Drew Adams <[hidden email]> wrote:
>> - Are there any objections to doing the buffer read-only check before
>>   doing the indent?
>
> Dunno what the right approach/solution is in this particular case.
>
> But note that we don't do what you suggest in general - we don't do it
> for most other editing operations: insert or delete a character, yank
> a string, kill the region,...  Instead, we let an error be raised,
> telling the user that the buffer is read-only.

The other commands you list finish pretty much instantaneously, so the
difference is somewhat academic there. Although yank has (interactive
"*P"), the star means the read-only check is at the beginning. So you
can't successfully yank the empty string into a read-only buffer.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RFC] Do indent-region only on editable buffers

Stefan Monnier
In reply to this post by Kaushal Modi
> The proposal is to call (barf-if-buffer-read-only) before attempting to do
> the indent.

No objection here.

> change is required? Here's what's will happen: 1. User: Try indentation
> (C-x h M-x indent-region) 2. User: Could take several seconds or few
> minutes (depending on major mode and file size) 3. Emacs: "Bummer, couldn't
> save all that indentation because the buffer is read-only". 4. User: Make

Indentation of a region isn't done by computing the complete result and
then "saving" it into the buffer.  So the error will be signaled as soon
as the indent-region finds a line whose indentation should be changed.
Usually that's almost instantaneous, but indeed in some cases it can
take a while (e.g. in large regions that are already indented except for
the last few lines).


        Stefan


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RFC] Do indent-region only on editable buffers

Kaushal Modi
On Mon, Aug 7, 2017 at 3:55 PM Stefan Monnier <[hidden email]> wrote:
> The proposal is to call (barf-if-buffer-read-only) before attempting to do
> the indent.

No objection here.

Thanks.
 
> change is required? Here's what's will happen: 1. User: Try indentation
> (C-x h M-x indent-region) 2. User: Could take several seconds or few
> minutes (depending on major mode and file size) 3. Emacs: "Bummer, couldn't
> save all that indentation because the buffer is read-only". 4. User: Make

Indentation of a region isn't done by computing the complete result and
then "saving" it into the buffer.  So the error will be signaled as soon
as the indent-region finds a line whose indentation should be changed.
Usually that's almost instantaneous, but indeed in some cases it can
take a while (e.g. in large regions that are already indented except for
the last few lines).

Thanks for explaining the behavior. That explains why the read-only buffer error is not thrown if the buffer is already indented. 
--

Kaushal Modi

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RFC] Do indent-region only on editable buffers

John Wiegley
In reply to this post by Stefan Monnier
>>>>> "SM" == Stefan Monnier <[hidden email]> writes:

>> The proposal is to call (barf-if-buffer-read-only) before attempting to do
>> the indent.

SM> No objection here.

Nor here.

--
John Wiegley                  GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com                          60E1 46C4 BD1A 7AC1 4BA2

Loading...