Skip to main content
Included result of comment discussion: flag-based application of the write blocking
Source Link
B Layer
  • 20.1k
  • 2
  • 32
  • 59

ThisUpdate: OP asked about using this only on designated buffers rather than any and all buffers. My suggestion was to repurpose b:blockwrite as a user-controlled flag. It is not heavily testeddefined by default and manually set to 1 for buffers to be protected. That responsibility fallswould require just a few changes to you as "payment"the function. The first conditional block is removed and the second conditional is modified. That gives us...

func! CheckWriteBlock(bufname)
    " Flag not defined or it's disabled (equal to 0)
    if !exists("b:blockwrite") || !b:blockwrite
        " Write the file and unset modified flag
        w
        set nomodified
        return
    endif

    " Last block remains the same so
    ...snip...
endfunc

I'd also create a convenience mapping for receiving this codetoggling the flag, e.g. :) I will, of course, look at anything that isn't working and help refine it as needed...

nnoremap <leader>b :let b:blockwrite = exists("b:blockwrite") ? !b:blockwrite : 1<cr>

This is not heavily tested. That responsibility falls to you as "payment" for receiving this code. :) I will, of course, look at anything that isn't working and help refine it as needed.

Update: OP asked about using this only on designated buffers rather than any and all buffers. My suggestion was to repurpose b:blockwrite as a user-controlled flag. It is not defined by default and manually set to 1 for buffers to be protected. That would require just a few changes to the function. The first conditional block is removed and the second conditional is modified. That gives us...

func! CheckWriteBlock(bufname)
    " Flag not defined or it's disabled (equal to 0)
    if !exists("b:blockwrite") || !b:blockwrite
        " Write the file and unset modified flag
        w
        set nomodified
        return
    endif

    " Last block remains the same so
    ...snip...
endfunc

I'd also create a convenience mapping for toggling the flag, e.g. ...

nnoremap <leader>b :let b:blockwrite = exists("b:blockwrite") ? !b:blockwrite : 1<cr>
works after buffer change
Source Link
B Layer
  • 20.1k
  • 2
  • 32
  • 59

Your question is pretty light on the details but I was in the mood for some vimscripting so this is my interpretation...

" How many minutes to unblock writes
let g:allowwritemins = 5

func! CheckWriteBlock(bufname)
    if !exists("b:blockwrite")
        let b:blockwrite = 1
    endif

    if !b:blockwrite
        " Not currently blocked. Write the file and unset modified flag
        w
        set nomodified
        return
    endif

    if input(printf("Warning: Writes are blocked. Enable writes for the next %d minutes? ", g:allowwritemins)) =~? "y"
        let b:blockwrite = 0
        " This will start a timer for configured number of minutes
        " after which blockwrite flag will be reset.
        call timer_start(g:allowwritemins * 60 * 1000, {-> executesetbufvar("let ba:blockwritebufname, ="blockwrite", 1"1)})
        redraw
        echom "You may write the file now."
    endif
endfunc

augroup writeblock
  autocmd!
  " BufWriteCmd is called before the write and requires the coder to
  " do the write themselves if that's what they want.
  autocmd BufWriteCmd * call CheckWriteBlock(bufname())
augroup END

Put all of that in your vimrc and for each file that you try to write it will block and ask you if you want to unblock writes. If you enter Y (case insensitive; followed by Enter) then for the configured number of minutes you can then do :w.

This is not heavily tested. That responsibility falls to you as "payment" for receiving this code. :) I will, of course, look at anything that isn't working and help refine it as needed.

Your question is pretty light on the details but I was in the mood for some vimscripting so this is my interpretation...

" How many minutes to unblock writes
let g:allowwritemins = 5

func! CheckWriteBlock()
    if !exists("b:blockwrite")
        let b:blockwrite = 1
    endif

    if !b:blockwrite
        " Not currently blocked. Write the file and unset modified flag
        w
        set nomodified
        return
    endif

    if input(printf("Warning: Writes are blocked. Enable writes for the next %d minutes? ", g:allowwritemins)) =~? "y"
        let b:blockwrite = 0
        " This will start a timer for configured number of minutes
        " after which blockwrite flag will be reset.
        call timer_start(g:allowwritemins * 60 * 1000, {-> execute("let b:blockwrite = 1")})
        redraw
        echom "You may write the file now."
    endif
endfunc

augroup writeblock
  autocmd!
  " BufWriteCmd is called before the write and requires the coder to
  " do the write themselves if that's what they want.
  autocmd BufWriteCmd * call CheckWriteBlock()
augroup END

Put all of that in your vimrc and for each file that you try to write it will block and ask you if you want to unblock writes. If you enter Y (case insensitive; followed by Enter) then for the configured number of minutes you can then do :w.

This is not heavily tested. That responsibility falls to you as "payment" for receiving this code. :) I will, of course, look at anything that isn't working and help refine it as needed.

Your question is pretty light on the details but I was in the mood for some vimscripting so this is my interpretation...

" How many minutes to unblock writes
let g:allowwritemins = 5

func! CheckWriteBlock(bufname)
    if !exists("b:blockwrite")
        let b:blockwrite = 1
    endif

    if !b:blockwrite
        " Not currently blocked. Write the file and unset modified flag
        w
        set nomodified
        return
    endif

    if input(printf("Warning: Writes are blocked. Enable writes for the next %d minutes? ", g:allowwritemins)) =~? "y"
        let b:blockwrite = 0
        " This will start a timer for configured number of minutes
        " after which blockwrite flag will be reset.
        call timer_start(g:allowwritemins * 60 * 1000, {-> setbufvar(a:bufname, "blockwrite", 1)})
        redraw
        echom "You may write the file now."
    endif
endfunc

augroup writeblock
  autocmd!
  " BufWriteCmd is called before the write and requires the coder to
  " do the write themselves if that's what they want.
  autocmd BufWriteCmd * call CheckWriteBlock(bufname())
augroup END

Put all of that in your vimrc and for each file that you try to write it will block and ask you if you want to unblock writes. If you enter Y (case insensitive; followed by Enter) then for the configured number of minutes you can then do :w.

This is not heavily tested. That responsibility falls to you as "payment" for receiving this code. :) I will, of course, look at anything that isn't working and help refine it as needed.

added redraw command so second message shows nicely
Source Link
B Layer
  • 20.1k
  • 2
  • 32
  • 59

Your question is pretty light on the details but I was in the mood for some vimscripting so this is my interpretation...

" How many minutes to unblock writes
let g:allowwritemins = 5

func! CheckWriteBlock()
    if !exists("b:blockwrite")
        let b:blockwrite = 1
    endif

    if !b:blockwrite
        " Not currently blocked. Write the file and unset modified flag
        w
        set nomodified
        return
    endif

    if input(printf("Warning: Writes are blocked. Enable writes for the next %d minutes? ", g:allowwritemins)) =~? "y"
        let b:blockwrite = 0
        " This will start a timer for configured number of minutes
        " after which blockwrite flag will be reset.
        call timer_start(g:allowwritemins * 60 * 1000, {-> execute("let b:blockwrite = 1")})
        redraw
        echom "You may write the file now."
    endif
endfunc

augroup writeblock
  autocmd!
  " BufWriteCmd is called before the write and requires the coder to
  " do the write themselves if that's what they want.
  autocmd BufWriteCmd * call CheckWriteBlock()
augroup END

Put all of that in your vimrc and for each file that you try to write it will block and ask you if you want to unblock writes. If you enter Y (case insensitive) (followedinsensitive; followed by Enter) then for the configured number of minutes you can then do :w.

This is not heavily tested and certainly not production grade. That responsibility falls to you as "payment" for receiving this code. :) I will, of course, look at anything that isn't working and help refine it as needed.

Your question is pretty light on the details but I was in the mood for some vimscripting so this is my interpretation...

" How many minutes to unblock writes
let g:allowwritemins = 5

func! CheckWriteBlock()
    if !exists("b:blockwrite")
        let b:blockwrite = 1
    endif

    if !b:blockwrite
        " Not currently blocked. Write the file and unset modified flag
        w
        set nomodified
        return
    endif

    if input(printf("Warning: Writes are blocked. Enable writes for the next %d minutes? ", g:allowwritemins)) =~? "y"
        let b:blockwrite = 0
        " This will start a timer for configured number of minutes
        " after which blockwrite flag will be reset.
        call timer_start(g:allowwritemins * 60 * 1000, {-> execute("let b:blockwrite = 1")})
        echom "You may write the file now."
    endif
endfunc

augroup writeblock
  autocmd!
  " BufWriteCmd is called before the write and requires the coder to
  " do the write themselves if that's what they want.
  autocmd BufWriteCmd * call CheckWriteBlock()
augroup END

Put all of that in your vimrc and for each file that you try to write it will block and ask you if you want to unblock writes. If you enter Y (case insensitive) (followed by Enter) then for the configured number of minutes you can then do :w.

This is not heavily tested and certainly not production grade. That responsibility falls to you as "payment" for receiving this code. :)

Your question is pretty light on the details but I was in the mood for some vimscripting so this is my interpretation...

" How many minutes to unblock writes
let g:allowwritemins = 5

func! CheckWriteBlock()
    if !exists("b:blockwrite")
        let b:blockwrite = 1
    endif

    if !b:blockwrite
        " Not currently blocked. Write the file and unset modified flag
        w
        set nomodified
        return
    endif

    if input(printf("Warning: Writes are blocked. Enable writes for the next %d minutes? ", g:allowwritemins)) =~? "y"
        let b:blockwrite = 0
        " This will start a timer for configured number of minutes
        " after which blockwrite flag will be reset.
        call timer_start(g:allowwritemins * 60 * 1000, {-> execute("let b:blockwrite = 1")})
        redraw
        echom "You may write the file now."
    endif
endfunc

augroup writeblock
  autocmd!
  " BufWriteCmd is called before the write and requires the coder to
  " do the write themselves if that's what they want.
  autocmd BufWriteCmd * call CheckWriteBlock()
augroup END

Put all of that in your vimrc and for each file that you try to write it will block and ask you if you want to unblock writes. If you enter Y (case insensitive; followed by Enter) then for the configured number of minutes you can then do :w.

This is not heavily tested. That responsibility falls to you as "payment" for receiving this code. :) I will, of course, look at anything that isn't working and help refine it as needed.

Source Link
B Layer
  • 20.1k
  • 2
  • 32
  • 59
Loading