From 7860c3506ae0dcc0c8c786e311452b7b19885a58 Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.uk>
Date: Sun, 9 Jan 2022 23:22:04 +0000
Subject: [PATCH] feat(nvim): add function to write and source files

Copied from tjdevries' config_manager repository, add a global
`save_and_exec` function that will write and source vim or lua files,
and use this within my existing `<leader>so` key mapping.
---
 roles/neovim/files/autoload/opdavies.vim    | 12 ++++++++++++
 roles/neovim/files/lua/opdavies/options.lua |  2 +-
 2 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 roles/neovim/files/autoload/opdavies.vim

diff --git a/roles/neovim/files/autoload/opdavies.vim b/roles/neovim/files/autoload/opdavies.vim
new file mode 100644
index 00000000..449666bf
--- /dev/null
+++ b/roles/neovim/files/autoload/opdavies.vim
@@ -0,0 +1,12 @@
+if !exists('*opdavies#save_and_exec')
+  function! opdavies#save_and_exec() abort
+    if &filetype == 'vim'
+      :silent! write
+      :source %
+    elseif &filetype == 'lua'
+      :silent! write
+      :luafile %
+    endif
+    return
+  endfunction
+endif
diff --git a/roles/neovim/files/lua/opdavies/options.lua b/roles/neovim/files/lua/opdavies/options.lua
index 935e400d..631ccc41 100644
--- a/roles/neovim/files/lua/opdavies/options.lua
+++ b/roles/neovim/files/lua/opdavies/options.lua
@@ -27,7 +27,7 @@ local function set_key_mappings()
 
   local options = { noremap = true }
 
-  map("n", "<Leader>so", ":luafile ~/.config/nvim/init.lua<Cr>", options)
+  map("n", "<Leader>so", ":call opdavies#save_and_exec()<CR>", options)
 
   -- Format paragraphs to an 80 character line length.
   map("n", "<Leader>g", "gqap", options)