Overview#

This module automates specific behaviors based on file types, buffer
events, or external system changes. It ensures that Markdown files
are formatted correctly, color themes (Pywal) are synchronized, and
the Kitty terminal font switches dynamically between coding and
writing modes.

-- ~/.config/nvim/lua/config/autocmds.lua
-- ------------------------------------
-- Markdown Settings
-- ------------------------------------
-- Markdown textwidth (Define when Linebreaks occur)
-- ------------------------------------
vim.api.nvim_create_autocmd({"FileType", "BufEnter"}, {
	pattern = "*.md",
	callback = function()
		if vim.bo.filetype == "markdown" then
			vim.opt_local.textwidth = 70
		end
	end,
})

-- CMD's for Markdown files
vim.api.nvim_create_autocmd("FileType", {
	pattern = "markdown",
	callback = function()
		vim.cmd("SoftPencil")
	end,
})

-- ------------------------------------
-- MISC
-- ------------------------------------
-- Pywal auto-reload
-- ------------------------------------
vim.api.nvim_create_autocmd({ "BufWritePost", "FileChangedShell" }, {
	group = vim.api.nvim_create_augroup("PywalAutoReload", { clear = true }),
	pattern = vim.fn.expand("~/.cache/wal/colors-wal.vim"),
	callback = function()
		if vim.fn.filereadable(vim.fn.expand("~/.cache/wal/colors-wal.vim")) == 1 then
			vim.cmd("silent! source ~/.cache/wal/colors-wal.vim")
		end
	end,
})


-- ------------------------------------
-- User commands
-- ------------------------------------
-- Convert .md to PDF
-- ------------------------------------
vim.api.nvim_create_user_command("ConvertToPDF", function()
	local choice = vim.fn.inputlist({
		"Select template:",
		"1. University (uni.latex)",
		"2. Simple (simple.latex)"
	})

	if choice < 1 or choice > 2 then return end

	local templates = { "uni", "simple" }
	local script = vim.fn.expand("~/bin/md2pdf.sh")

	if vim.fn.filereadable(script) == 1 then
		vim.cmd("lcd %:p:h | !" .. script .. " %:p " .. templates[choice])
	else
		vim.notify("md2pdf.sh not found", vim.log.levels.ERROR)
	end
end, {})

-- ------------------------------------
-- --- Kitty Font Switch for .md --- --
-- ------------------------------------
local default_font_conf = "~/.config/kitty/font_default.conf"
local markdown_font_conf = "~/.config/kitty/font_markdown.conf"

local function switch_kitty_font()
  local current_ft = vim.bo.filetype
  local buf_name = vim.api.nvim_buf_get_name(0)

  -- 1. to .md font
  if current_ft == "markdown" then
    local cmd = string.format("kitty @ load-config %s", markdown_font_conf)
    vim.schedule(function() vim.fn.system(cmd) end)
    return
  end

  -- 2. Wenn wir im Prose-Analyzer oder in der Toolbar sind: TU NICHTS.
  -- Das behält die aktuelle Schriftart (Markdown) einfach bei.
  if current_ft == "prose_summary" or buf_name:find("ProseAnalyzer Summary") or current_ft == "" then
    return 
  end

  -- 3. Liste standard font
  local force_default_ft = {
    ["lua"] = true,
    ["sh"] = true,
    ["python"] = true,
    ["conf"] = true,
    ["kitty"] = true,
    ["help"] = true,
  }

  -- 4. set back 
  if force_default_ft[current_ft] then
    local cmd = string.format("kitty @ load-config %s", default_font_conf)
    vim.schedule(function() vim.fn.system(cmd) end)
  end
end

local font_switcher_group = vim.api.nvim_create_augroup("KittyFontSwitcher", { clear = true })

-- toggle switch
vim.api.nvim_create_autocmd({ "FileType", "BufWinEnter" }, {
  pattern = "*",
  group = font_switcher_group,
  callback = function()
    switch_kitty_font()
  end,
})

-- back to default on quit
vim.api.nvim_create_autocmd("VimLeave", {
  group = font_switcher_group,
  callback = function()
    local cmd = string.format("kitty @ load-config %s", default_font_conf)
    vim.fn.system(cmd)
  end,
})