Kakoune: Buffer is not automatically detected as readonly

Created on 7 Jul 2018  ·  8Comments  ·  Source: mawww/kakoune

I want to add a marker to my modeline if a buffer is readonly, but kakoune fails to detect it automatically.

Steps

❯ ls -al /etc/resolv.conf                 
.rw-r--r-- 52 root  7 Jul 11:16 /etc/resolv.conf

❯ kak /etc/resolv.conf
:echo %sh{ echo $kak_opt_readonly } -> false (should be true)
:w                                  -> 1:1: 'w' /etc/resolv.conf: Operation not permitted

nvim correctly detects the status

❯ nvim /etc/resolv.conf
:set readonly?                      -> readonly

Most helpful comment

I tested a bit more, in kakoune if a buffer is marked with readonly option, I can still save it to a different file with :w <file>, but surprisingly I cannot save it with :w!.

I think the semantics of nvim make more sense to me, where readonly option is correlated with :w and :w! is there to try to bypass the readonly status of a buffer, regardless of how it was set.

All 8 comments

Its not clear to me that we want to prevent modifying buffers if the file associated with them is not writable. First we have the write! command which tries to force write the file (it will try to change the file permission to allow writing and then write the file, which works for read-only files that belong to the current user), so setting those as readonly would be pretty inconsistent. Second we might just want to modify the buffer and write it to another file (with write <filename>).

I am not sure if setting the buffer read-only by default is the way to go, thats not a very strong opinion though, discussion welcome.

I see, so there is currently a difference in semantics of readonly between kakoune and nvim. In nvim, readonly flag means "the buffer cannot be written with :w", even though nvim also has :w! (and it will successfully be able to write a readonly file that belongs to the current user) and :w <filename>.

I'll explain why I created this ticket. I want to put the value of readonly in my modeline, so that I am notified when a file cannot be written with :w. I _know_ I can try to save it with :w!, or save to a different file, or pipe through sudo tee... but in most cases I am opening a file in order to save it with :w, and it is super annoying to discover _after_ I made all my modifications that I cannot simply save the file. I want a lazy approach, open the file, immediately see the readonly marker, if I intend to modify the file, close and reopen with sudo.

I tested a bit more, in kakoune if a buffer is marked with readonly option, I can still save it to a different file with :w <file>, but surprisingly I cannot save it with :w!.

I think the semantics of nvim make more sense to me, where readonly option is correlated with :w and :w! is there to try to bypass the readonly status of a buffer, regardless of how it was set.

For what it's worth, (n)vim uses two different options for this: readonly means the file isn't writable, and modifiable means the buffer can be modified. It warns you the first time you enter insert mode for a readonly file, but still lets you modify the buffer (and write it with :w!).

What would be your personal preference? In my mind, having both readonly and modifiable is a bit of an overkill, it's enough having the readonly option for marking the buffer as one that cannot be saved with :w (for whatever reason, manually marking, or because the file itself is readonly), but then make :w! ignore the readonly status and try its best to save the file.

I don't care too much. I think the vim behavior is reasonable. "modifiable" is turned off for things like :help pages, which is how kak uses "readonly" right now. I just think it should be clear what the option is intended to mean.

I think kakoune's current readonly definition is fine.

So readonly in kakoune (and modifiable in vim) is the way to purposefully restrict editing a buffer, even if it is actually linked to a writeable file — :help is a perfect example of why this is needed. I wish the documentation for readonly could be more explicit about this.

And what I wanted (a modeline indicator if a file cannot be saved with :w) is actually very easy to achieve with this:

%sh{ [[ -f "$kak_buffile" && ! -w "$kak_buffile" ]] && echo "[RO]" }

Thus closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akkartik picture akkartik  ·  3Comments

abitofalchemy picture abitofalchemy  ·  3Comments

basbebe picture basbebe  ·  4Comments

lenormf picture lenormf  ·  4Comments

radare picture radare  ·  3Comments