autosave.vim

vim plugin to autosave buffer when leaving cursor idle
Index Commits Files Refs README LICENSE
commit 76a131e1632207a5dfbe55d41079b39a700a696d
Author: mjkloeckner <martinjkloeckner@gmail.com>
Date:   Fri, 28 Apr 2023 21:17:24 -0300

first commit

Diffstat:
ALICENSE | 21+++++++++++++++++++++
AREADME.md | 32++++++++++++++++++++++++++++++++
Aplugin/autosave.vim | 33+++++++++++++++++++++++++++++++++
3 files changed, 86 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2023 Martin J. Klöckner
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
@@ -0,0 +1,32 @@
+# vim autosave
+
+This is a plugin to auto save the current buffer (if modified) when the cursor
+is idle.
+
+## Installing
+
+If you are using [vim-plug](https://github.com/junegunn/vim-plug) add this to
+your plugins list:
+
+```vim
+    Plug 'chrisbra/Colorizer'
+```
+
+## Options
+
+- Enable auto_save for only these filetypes, to see all posible filetypes 
+  see: <https://vi.stackexchange.com/questions/5780/list-known-filetypes>
+
+  ```vim
+  let g:auto_save_file_types = [ 'markdown' ]
+  ```
+
+- Enable auto_save for all filetypes
+  ```vim
+  let g:auto_save_all_filetypes = 0
+  ```
+
+
+## License
+
+[MIT](https://opensource.org/licenses/MIT)
diff --git a/plugin/autosave.vim b/plugin/autosave.vim
@@ -0,0 +1,33 @@
+" default filetypes
+if !exists('g:auto_save_file_types')
+    let g:auto_save_file_types = [ 'markdown', 'text' ]
+endif
+
+let s:auto_save_filetype_enabled = 0
+let s:auto_save_write = 0
+
+function s:auto_save()
+    if s:auto_save_filetype_enabled == 1 || g:auto_save_all_filetypes == 1
+        if s:auto_save_write == 1
+            let s:auto_save_write = 0
+            if &modified
+                let l:bytes = line2byte('$') + len(getline('$'))
+                if filereadable(expand('%:p')) == 1
+                    silent! write
+                    echo "\"" .. expand('%') .. "\" " .. line('$') .. "L, " ..
+                                \ l:bytes .. "B written [autosave]"
+                else
+                    silent! write
+                    echo "\"" .. expand('%') .. "\" [New] " .. line('$') ..
+                                \"L, " .. l:bytes .. "B written [autosave]"
+                endif
+            endif
+        endif
+    endif
+endfunction
+
+au TextChanged,TextChangedI <buffer> silent! let s:auto_save_write = 1
+au CursorHold,CursorHoldI <buffer> call s:auto_save()
+
+au BufEnter,FileType * if index(g:auto_save_file_types, &filetype) !=# -1 |
+            \ let s:auto_save_filetype_enabled = 1 | endif