summary refs log tree commit diff
path: root/.vim/plugin
diff options
context:
space:
mode:
Diffstat (limited to '.vim/plugin')
-rw-r--r--.vim/plugin/colorscheme.lua23
-rw-r--r--.vim/plugin/darkmode.lua47
2 files changed, 47 insertions, 23 deletions
diff --git a/.vim/plugin/colorscheme.lua b/.vim/plugin/colorscheme.lua
deleted file mode 100644
index 240737e..0000000
--- a/.vim/plugin/colorscheme.lua
+++ /dev/null
@@ -1,23 +0,0 @@
-vim.opt.background = 'dark'
-
--- setup color-scheme detection via desktop portal apis
-vim.api.nvim_create_autocmd('UIEnter', {
-  once = true,
-  callback = function()
-    local job = require('plenary.job'):new({
-      command = 'porta',
-      args = { 'settings', 'org.freedesktop.appearance', 'color-scheme' },
-      enable_recording = true,
-    })
-    local res, exitcode = job:sync()
-    if exitcode == 0 and #res == 1 then
-      if res[1] == 'prefer-light' then
-        vim.opt.background = 'light'
-      else
-        vim.opt.background = 'dark'
-      end
-    end
-  end,
-})
-
-vim.api.nvim_set_hl(0, 'ColorColumn', { link = 'CursorColumn', force = true })
diff --git a/.vim/plugin/darkmode.lua b/.vim/plugin/darkmode.lua
new file mode 100644
index 0000000..6ffd06f
--- /dev/null
+++ b/.vim/plugin/darkmode.lua
@@ -0,0 +1,47 @@
+-- this plugin registers registers and autocommand that switches colorschemes on UIEnter based on
+-- the system's preference for dark/light mode
+
+local state = { enabled = true }
+
+if not state.enabled then
+  return
+end
+
+local set_darkmode = function(enabled)
+  if enabled then
+    vim.opt.background = 'dark'
+    vim.cmd.colorscheme(vim.g.darkmode_colorscheme_dark or 'default')
+  else
+    vim.opt.background = 'light'
+    vim.cmd.colorscheme(vim.g.darkmode_colorscheme_light or 'default')
+  end
+end
+
+-- returns if system is configured to use darkmode
+local discover = function()
+  require('plenary.job')
+    :new({
+      command = 'porta',
+      args = { 'settings', 'org.freedesktop.appearance', 'color-scheme' },
+      enable_recording = true,
+      on_exit = vim.schedule_wrap(function(job, exitcode)
+        local darkmode_enabled = true
+        local res = job:result()
+        if exitcode == 0 and #res then
+          darkmode_enabled = not (res[1] == 'prefer-light')
+        end
+        set_darkmode(darkmode_enabled)
+      end),
+    })
+    :start()
+end
+
+-- setup autogroup
+local aug = vim.api.nvim_create_augroup('darkmode', { clear = true })
+
+-- setup color-scheme detection via desktop portal apis
+vim.api.nvim_create_autocmd({ 'UIEnter', 'VimResume' }, { callback = discover, group = aug })
+vim.api.nvim_create_autocmd({ 'Signal' }, { pattern = 'SIGUSR1', callback = discover, group = aug })
+
+-- fix ColorColumn not being defined
+vim.api.nvim_set_hl(0, 'ColorColumn', { link = 'CursorColumn', force = true })