function ThemeUpdate()
    local scheme_name = "tokyodark" -- default if unable to parse from file
    local current_theme_file = vim.fn.stdpath("config") .. "/current-theme"
    local ok, theme = pcall(dofile, current_theme_file)
    if ok then
        scheme_name = theme.colorscheme_name
    end
    vim.cmd.colorscheme(scheme_name)
    pcall(dofile, vim.fn.stdpath('config') .. '/lua/theme_transparency.lua')
end

function TmpBuff(split_opt)
    local new_cmd = split_opt or "enew"
    vim.cmd(new_cmd)
    vim.opt_local.buftype = "nofile"
    vim.opt_local.bufhidden = "hide"
    vim.opt_local.swapfile = false
    vim.cmd("file tmp_" .. os.date("%Y%m%d_%H%M%S") .. "_" .. math.random(471))
end

function ReadShellCmd(command)
    vim.cmd("read !" .. command)
end

function ToggleTabsSpaces()
    if vim.opt.expandtab:get() then
        vim.opt.expandtab = false
        print("using actual tabs")
    else
        vim.opt.expandtab = true
        print("using spaces in place of tabs")
    end
end

function ToggleLineNumbers()
    if vim.opt.number:get() then
        vim.opt.number = false
        vim.opt.relativenumber = false
    else
        vim.opt.number = true
        vim.opt.relativenumber = true
    end
end

function ToggleColorColumn(column_string)
    if #vim.opt.colorcolumn:get() == 0 then
        vim.opt.colorcolumn = (column_string or '90')
    else
        vim.opt.colorcolumn = ''
    end
end

function ToggleCursorLine()
    vim.opt.cursorline = (not vim.opt.cursorline:get())
end

local function EnableWritingModeUiForCurrentWindow()
    vim.opt_local.number = false
    vim.opt_local.relativenumber = false
    vim.opt_local.colorcolumn = ''
    vim.opt_local.signcolumn = "no"
    vim.opt_local.cursorline = false
    vim.opt_local.winfixwidth = true
    -- vim.opt_local.wrap = true -- TODO: needed?
    vim.opt_local.laststatus = 0
end

function ToggleWritingMode()
    if vim.g.writing_mode then
        vim.g.writing_mode = false

        vim.api.nvim_set_hl(0, 'WinSeparator', vim.g.wrmode_prev_hl_winsep or {})
        vim.opt_local.winfixwidth = false
        vim.cmd("wincmd l")
        vim.api.nvim_win_close(0, false)
        vim.cmd("wincmd h")
        vim.api.nvim_win_close(0, false)

        vim.opt_local.number = true
        vim.opt_local.relativenumber = true
        vim.opt_local.signcolumn = "yes"
        vim.opt_local.cursorline = true
        vim.api.nvim_set_hl(0, 'SpellCap', vim.g.wrmode_prev_hl_spellcap or {})
        vim.opt_local.spell = false
        -- vim.opt_local.wrap = true -- TODO: needed?
        vim.opt_local.textwidth = 0
        vim.opt_local.scrolloff = 2
        vim.opt_local.formatoptions:remove('t')
        vim.opt_local.laststatus = 0
        -- vim.cmd("vertical resize")
    else
        vim.g.writing_mode = true

        vim.g.wrmode_prev_hl_winsep = vim.api.nvim_get_hl(0, { name = 'WinSeparator' })
        vim.api.nvim_set_hl(0, 'WinSeparator', { bg = 'none', fg = '#000000' })
        local window_width = 72
        local window_padding = math.max(math.floor((vim.o.columns - window_width) / 2), 1)
        vim.cmd("vsplit")
        local writing_pane = vim.api.nvim_get_current_win()
        EnableWritingModeUiForCurrentWindow()
        vim.opt_local.spell = true
        vim.g.wrmode_prev_hl_spellcap = vim.api.nvim_get_hl(0, { name = 'SpellCap' })
        vim.api.nvim_set_hl(0, 'SpellCap', {})
        vim.opt_local.textwidth = window_width
        vim.opt_local.scrolloff = 14
        vim.opt_local.formatoptions:append('t')
        vim.cmd("wincmd h")
        TmpBuff('enew')
        local padding_pane_left = vim.api.nvim_get_current_win()
        EnableWritingModeUiForCurrentWindow()
        vim.cmd("wincmd l")
        TmpBuff('vnew')
        local padding_pane_right = vim.api.nvim_get_current_win()
        EnableWritingModeUiForCurrentWindow()
        vim.api.nvim_set_current_win(writing_pane)

        vim.api.nvim_win_set_width(padding_pane_left, window_padding)
        vim.api.nvim_win_set_width(padding_pane_right, window_padding)
        vim.api.nvim_win_set_width(writing_pane, window_width)
    end
end

