From f654cc1c04ef99a7c1e9f8664f8bba3778e72091 Mon Sep 17 00:00:00 2001
From: Jannik Buhr <jannik.m.buhr@gmail.com>
Date: Sun, 22 Dec 2024 18:49:44 +0100
Subject: [PATCH] feat: detect editor: render-on-save: false

---
 lua/quarto/init.lua | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/lua/quarto/init.lua b/lua/quarto/init.lua
index fe13cd7..74fcd30 100644
--- a/lua/quarto/init.lua
+++ b/lua/quarto/init.lua
@@ -17,6 +17,37 @@ function M.quartoPreview(opts)
   local cmd
   local mode
 
+
+  -- check for 
+  --
+  -- editor:
+  --   render-on-save: false
+  --
+  -- in _quarto.yml or the current qmd file
+
+  local render_on_save = true
+
+  local lines
+  if root_dir then
+    local quarto_config = root_dir .. '/_quarto.yml'
+    lines = vim.fn.readfile(quarto_config)
+  else
+    -- assumption: the yaml header is not longer than a generous 500 lines 
+    lines = vim.api.nvim_buf_get_lines(0, 0, 500, false)
+  end
+
+  local query = 'render%-on%-save: false'
+  for _, line in ipairs(lines) do
+    if line:find(query) then
+      render_on_save = false
+      break
+    end
+  end
+
+  if not render_on_save and string.find(args, '%-%-no%-watch%-inputs') == nil then
+    args = args .. ' --no-watch-inputs'
+  end
+
   if root_dir then
     mode = 'project'
     cmd = 'quarto preview ' .. args