aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsrdusr <trevorgray@srdusr.com>2023-09-07 13:01:11 +0200
committersrdusr <trevorgray@srdusr.com>2023-09-07 13:01:11 +0200
commitdecb9a33971e99db3dbb6d41c7757a07a7d4addc (patch)
tree36229ae7c7d8e8858d198776ba8c0c0c271e190f
parent6932db57d88ce2bab25c488f6a4afe37d19cccc7 (diff)
downloaddotfiles-decb9a33971e99db3dbb6d41c7757a07a7d4addc.tar.gz
dotfiles-decb9a33971e99db3dbb6d41c7757a07a7d4addc.zip
Intercept file open
-rw-r--r--lua/user/mods.lua112
1 files changed, 102 insertions, 10 deletions
diff --git a/lua/user/mods.lua b/lua/user/mods.lua
index e4b6b41..6420ed5 100644
--- a/lua/user/mods.lua
+++ b/lua/user/mods.lua
@@ -27,14 +27,14 @@ end
--------------------------------------------------
-- Format on save
-local augroup = vim.api.nvim_create_augroup('LspFormatting', {})
+local format_augroup = vim.api.nvim_create_augroup('LspFormatting', {})
require('null-ls').setup({
-- you can reuse a shared lspconfig on_attach callback here
on_attach = function(client, bufnr)
if client.supports_method('textDocument/formatting') then
- vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
+ vim.api.nvim_clear_autocmds({ group = format_augroup, buffer = bufnr })
vim.api.nvim_create_autocmd('BufWritePre', {
- group = augroup,
+ group = format_augroup,
buffer = bufnr,
callback = function()
-- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead
@@ -134,15 +134,9 @@ end
-- Define a global function to retrieve LSP clients based on Neovim version
function M.get_lsp_clients(bufnr)
local mods = require('user.mods')
- local expected_ver = '0.10.0'
+ --local expected_ver = '0.10.0'
local nvim_ver = mods.get_nvim_version()
- --if nvim_ver ~= expected_ver then
- -- local msg = string.format("Unsupported nvim version: expect %s, but got %s instead!", expected_ver, nvim_ver)
- -- vim.api.nvim_err_writeln(msg)
- -- return
- --end
-
local version_major, version_minor = string.match(nvim_ver, '(%d+)%.(%d+)')
version_major = tonumber(version_major)
version_minor = tonumber(version_minor)
@@ -720,5 +714,103 @@ end
--------------------------------------------------
+-- Intercept file open
+local augroup = vim.api.nvim_create_augroup('user-autocmds', { clear = true })
+local intercept_file_open = true
+vim.api.nvim_create_user_command('InterceptToggle', function()
+ intercept_file_open = not intercept_file_open
+ local intercept_state = '`Enabled`'
+ if not intercept_file_open then
+ intercept_state = '`Disabled`'
+ end
+ vim.notify('Intercept file open set to ' .. intercept_state, vim.log.levels.INFO, {
+ title = 'Intercept File Open',
+ ---@param win integer The window handle
+ on_open = function(win)
+ vim.api.nvim_buf_set_option(vim.api.nvim_win_get_buf(win), 'filetype', 'markdown')
+ end,
+ })
+end, { desc = 'Toggles intercepting BufNew to open files in custom programs' })
+
+-- NOTE: Add "BufReadPre" to the autocmd events to also intercept files given on the command line, e.g.
+-- `nvim myfile.txt`
+vim.api.nvim_create_autocmd({ 'BufNew' }, {
+ group = augroup,
+ callback = function(args)
+ ---@type string
+ local path = args.match
+ ---@type integer
+ local bufnr = args.buf
+
+ ---@type string? The file extension if detected
+ local extension = vim.fn.fnamemodify(path, ':e')
+ ---@type string? The filename if detected
+ local filename = vim.fn.fnamemodify(path, ':t')
+
+ ---Open a given file path in a given program and remove the buffer for the file.
+ ---@param buf integer The buffer handle for the opening buffer
+ ---@param fpath string The file path given to the program
+ ---@param fname string The file name used in notifications
+ ---@param prog string The program to execute against the file path
+ local function open_in_prog(buf, fpath, fname, prog)
+ vim.notify(string.format('Opening `%s` in `%s`', fname, prog), vim.log.levels.INFO, {
+ title = 'Open File in External Program',
+ ---@param win integer The window handle
+ on_open = function(win)
+ vim.api.nvim_buf_set_option(vim.api.nvim_win_get_buf(win), 'filetype', 'markdown')
+ end,
+ })
+ local mods = require('user.mods')
+ local nvim_ver = mods.get_nvim_version()
+
+ local version_major, version_minor = string.match(nvim_ver, '(%d+)%.(%d+)')
+ version_major = tonumber(version_major)
+ version_minor = tonumber(version_minor)
+
+ if version_major > 0 or (version_major == 0 and version_minor >= 10) then
+ vim.system({ prog, fpath }, { detach = true })
+ else
+ vim.fn.jobstart({ prog, fpath }, { detach = true })
+ end
+ vim.api.nvim_buf_delete(buf, { force = true })
+ end
+
+ local extension_callbacks = {
+ ['pdf'] = function(buf, fpath, fname)
+ open_in_prog(buf, fpath, fname, 'zathura')
+ end,
+ ['png'] = function(buf, fpath, fname)
+ open_in_prog(buf, fpath, fname, 'feh')
+ end,
+ ['jpg'] = 'png',
+ ['mp4'] = function(buf, fpath, fname)
+ open_in_prog(buf, fpath, fname, 'mpv')
+ end,
+ ['gif'] = 'mp4',
+ }
+
+ ---Get the extension callback for a given extension. Will do a recursive lookup if an extension callback is actually
+ ---of type string to get the correct extension
+ ---@param ext string A file extension. Example: `png`.
+ ---@return fun(bufnr: integer, path: string, filename: string?) extension_callback The extension callback to invoke, expects a buffer handle, file path, and filename.
+ local function extension_lookup(ext)
+ local callback = extension_callbacks[ext]
+ if type(callback) == 'string' then
+ callback = extension_lookup(callback)
+ end
+ return callback
+ end
+
+ if extension ~= nil and not extension:match('^%s*$') and intercept_file_open then
+ local callback = extension_lookup(extension)
+ if type(callback) == 'function' then
+ callback(bufnr, path, filename)
+ end
+ end
+ end,
+})
+
+--------------------------------------------------
+
-- ...
return M