Thursday, May 6, 2010

Re: au BufReadPost * if &readonly | set nomodifiable

On May 5, 3:40 pm, rudy_b <rudyke...@yahoo.com> wrote:
> thanks for your responses, but I tried everything and it still doen't work.
>
> I tired this:
> au BufReadPost * if &readonly | set nomodifiable | endif
>
> and this:
> autocmd BufRead,BufWinEnter * if &ft!='qf' | let &l:modifiable = (&readonly
> ? 0 : 1) | endif
>
> It only works fine for the very first buffer that I open in a window, but
> once I open a new readonly file (using "vsplit" or "sp") it just allows for
> modification....
>
> Any idea why this is happenning? or any possible solution around it?
>

What version of Vim are you using? The second one (which I supplied)
works fine in pretty much all cases for me. I open files using :vsp
filename, :new filename, :tabe filename, :e filename, :tabnew
filename, :drop filename, :tab drop filename, :sp and then :e
filename, drag-and-drop, and using Windows file associations all the
time. I can't think of anything that would interfere with something so
simple.

There is this, from :help BufWinEnter:

Does not happen for |:split| without
arguments, since you keep editing the same
buffer, or ":split" with a file that's already
open in a window, because it re-uses an
existing buffer. But it does happen for a
":split" with the name of the current buffer,
since it reloads that buffer.

However, using :split without a file name, followed by ":e filename",
should work fine, as the BufRead event will be triggered.

It may also be a good debugging idea, if you put the autocmd we
discussed, into its own file, say readonly.vim, and put this file in
your plugin directory. This way, when you load your new file, you can
":verbose set modifiable?" to see where the option was last set for
this particular file. It should point to readonly.vim. Otherwise, you
have something else setting this option.

Additionally, something I DIDN'T include in my original email, was
that I surround this particular autocmd (among others) with:

augroup file_handling
au!
...
...
augroup END

This (among other things I do) ensures that I can just :source
my .vimrc again to see any updates if I'm making changes. The SAFEST
way to test would be to restart Vim entirely. But you at least need to
do this if you're not restarting Vim between edits of your .vimrc.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: