summaryrefslogtreecommitdiff
path: root/lua/config/03-commands.lua
blob: f8b0d477987e7ee604e8238fddaeb3d4fe6ab672 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- Commands and command-line abbreviations migrated from the original init.vim.
-- This file intentionally does not register autocommands or keymaps.

local api = vim.api
local config_dir = vim.fn.stdpath('config')

-- Edit the new configuration (original commands opened init.vim/local.vim).
api.nvim_create_user_command('Config', function()
  vim.cmd.edit(vim.fn.fnameescape(config_dir .. '/init.lua'))
end, { desc = 'Edit Neovim init.lua' })

api.nvim_create_user_command('Configl', function()
  vim.cmd.edit(vim.fn.fnameescape(config_dir .. '/lua/config/local/00-machine.lua'))
end, { desc = 'Edit machine-local configuration' })

-- :W is an easy-to-type alias for :w. Pass through arguments and !.
api.nvim_create_user_command('W', function(opts)
  vim.cmd('write' .. (opts.bang and '!' or '') .. (opts.args ~= '' and ' ' .. opts.args or ''))
end, { bang = true, nargs = '*', complete = 'file', desc = 'Write buffer' })

-- Open a terminal in the specified directory, defaulting to the current
-- buffer's directory. The original function required a directory argument or
-- a buffer whose name was itself a directory; the default is more useful here.
_G.open_terminal_on_directory = function(dirname)
  dirname = dirname or vim.fn.expand('%:p:h')
  if vim.fn.isdirectory(dirname) ~= 1 then
    vim.notify('Not a directory: ' .. dirname, vim.log.levels.WARN)
    return
  end

  local shell_cmd = string.format('cd %s && exec "$SHELL"', vim.fn.shellescape(dirname))
  vim.cmd('terminal sh -c ' .. vim.fn.shellescape(shell_cmd))
  vim.cmd.startinsert()
  vim.cmd('silent! keepalt file ' .. vim.fn.fnameescape('TERM ' .. dirname))
end

api.nvim_create_user_command('TERM', function(opts)
  _G.open_terminal_on_directory(opts.args ~= '' and vim.fn.expand(opts.args) or nil)
end, { nargs = '?', complete = 'dir', desc = 'Terminal in current file directory' })

-- Kept global because 01-mappings.lua invokes it with :lua run_format_code().
_G.run_format_code = function()
  local clients = vim.lsp.get_clients({ bufnr = 0, method = 'textDocument/formatting' })
  if #clients > 0 then
    vim.lsp.buf.format({ async = false })
  elseif vim.fn.exists(':FormatCode') == 2 then
    vim.cmd('FormatCode')
  else
    vim.notify('No LSP formatter or :FormatCode command available', vim.log.levels.WARN)
  end
end

-- Developer conveniences from the original config.
_G.remove_package = function(prefix)
  for name in pairs(package.loaded) do
    if name:sub(1, #prefix) == prefix then
      package.loaded[name] = nil
    end
  end
end

_G.reload_package = function(name)
  _G.remove_package(name)
  return require(name)
end

-- Original command-line abbreviations. Limit them to a leading command-line
-- token, so they don't unexpectedly expand inside commands or search patterns.
local abbreviations = {
  _config = config_dir,
  _plugged = vim.fn.stdpath('data') .. '/plugged', -- legacy vim-plug path
  _fm = vim.fn.stdpath('data') .. '/plugged/fieldmarshal.vim', -- legacy path
  _home = vim.fn.stdpath('data'),
  _projects = vim.fn.expand('~/Projects'),
}

for word, replacement in pairs(abbreviations) do
  vim.cmd('cnoreabbrev <expr> ' .. word .. ' (getcmdtype() ==# ":" && getcmdline() ==# "' .. word .. '") ? ' .. vim.fn.string(replacement) .. ' : "' .. word .. '"')
end

vim.cmd([[cnoreabbrev <expr> _ (getcmdtype() ==# ':' && getcmdline() ==# '_') ? fnameescape(expand('%:h')) : '_']])

-- The old :UpdateInitVimHook belonged to vim-plug and is intentionally omitted.
-- Move the old InitVim/UseTerminal autocommands to a separate 04-autocmds.lua.