aboutsummaryrefslogtreecommitdiff
path: root/dot_config/herdr/plugins/github/herdr-splits-d880c4924f2b/lua/herdr-splits/init.lua
blob: 378209034800d64adf3487b3819bd6a531ae0606 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---herdr-splits.nvim — Seamless navigation and resizing between Neovim splits
---and Herdr panes. Makes Herdr terminal splits behave like native Neovim windows.
---
---Usage:
---   require('herdr-splits').setup({ ... })
---   vim.keymap.set('n', '<C-h>', require('herdr-splits').move_cursor_left)
---   vim.keymap.set('n', '<M-h>', require('herdr-splits').resize_left)

local config = require('herdr-splits.config')
local nav = require('herdr-splits.nav')
local resize_mod = require('herdr-splits.resize')
local win = require('herdr-splits.win')
local sync = require('herdr-splits.sync')

local M = {}

---Apply configuration. Idempotent — calling again merges into existing config.
---@param opts table|nil Configuration options (see config.lua for defaults)
function M.setup(opts)
  config.setup(opts)
  pcall(sync.sync)
end

---Sync the Herdr-managed checkout to match this lazy commit.
---Intended as a lazy.nvim `build` hook entry point; respects `auto_sync_herdr`.
---Safe to call at any time; no-op unless opted in.
function M.sync_herdr()
  pcall(sync.sync)
end

---Resize the current split to the left.
---Amount defaults to config.default_amount. Multiplied by vim.v.count1.
---@param amount number|nil
function M.resize_left(amount)
  local eventignore_orig = vim.o.eventignore
  vim.o.eventignore = table.concat(config.ignored_events, ',')
  resize_mod.is_resizing = true
  local cur_win = vim.api.nvim_get_current_win()
  local ok, err = pcall(resize_mod.resize, 'left', amount)
  if not ok then
    vim.notify('herdr-splits: resize_left failed: ' .. tostring(err), vim.log.levels.ERROR)
  end
  pcall(vim.api.nvim_set_current_win, cur_win)
  resize_mod.is_resizing = false
  vim.o.eventignore = eventignore_orig
end

---Resize the current split downward.
---@param amount number|nil
function M.resize_down(amount)
  local eventignore_orig = vim.o.eventignore
  vim.o.eventignore = table.concat(config.ignored_events, ',')
  resize_mod.is_resizing = true
  local cur_win = vim.api.nvim_get_current_win()
  local ok, err = pcall(resize_mod.resize, 'down', amount)
  if not ok then
    vim.notify('herdr-splits: resize_down failed: ' .. tostring(err), vim.log.levels.ERROR)
  end
  pcall(vim.api.nvim_set_current_win, cur_win)
  resize_mod.is_resizing = false
  vim.o.eventignore = eventignore_orig
end

---Resize the current split upward.
---@param amount number|nil
function M.resize_up(amount)
  local eventignore_orig = vim.o.eventignore
  vim.o.eventignore = table.concat(config.ignored_events, ',')
  resize_mod.is_resizing = true
  local cur_win = vim.api.nvim_get_current_win()
  local ok, err = pcall(resize_mod.resize, 'up', amount)
  if not ok then
    vim.notify('herdr-splits: resize_up failed: ' .. tostring(err), vim.log.levels.ERROR)
  end
  pcall(vim.api.nvim_set_current_win, cur_win)
  resize_mod.is_resizing = false
  vim.o.eventignore = eventignore_orig
end

---Resize the current split to the right.
---@param amount number|nil
function M.resize_right(amount)
  local eventignore_orig = vim.o.eventignore
  vim.o.eventignore = table.concat(config.ignored_events, ',')
  resize_mod.is_resizing = true
  local cur_win = vim.api.nvim_get_current_win()
  local ok, err = pcall(resize_mod.resize, 'right', amount)
  if not ok then
    vim.notify('herdr-splits: resize_right failed: ' .. tostring(err), vim.log.levels.ERROR)
  end
  pcall(vim.api.nvim_set_current_win, cur_win)
  resize_mod.is_resizing = false
  vim.o.eventignore = eventignore_orig
end

---Move cursor to the left, crossing into Herdr pane if at Neovim edge.
---@param opts table|nil { same_row: boolean|nil, at_edge: string|function|nil }
function M.move_cursor_left(opts)
  resize_mod.is_resizing = false
  local ok, err = pcall(nav.move_cursor, 'left', opts)
  if not ok then
    vim.notify('herdr-splits: move_cursor_left failed: ' .. tostring(err), vim.log.levels.ERROR)
  end
end

---Move cursor downward, crossing into Herdr pane if at Neovim edge.
---@param opts table|nil
function M.move_cursor_down(opts)
  resize_mod.is_resizing = false
  local ok, err = pcall(nav.move_cursor, 'down', opts)
  if not ok then
    vim.notify('herdr-splits: move_cursor_down failed: ' .. tostring(err), vim.log.levels.ERROR)
  end
end

---Move cursor upward, crossing into Herdr pane if at Neovim edge.
---@param opts table|nil
function M.move_cursor_up(opts)
  resize_mod.is_resizing = false
  local ok, err = pcall(nav.move_cursor, 'up', opts)
  if not ok then
    vim.notify('herdr-splits: move_cursor_up failed: ' .. tostring(err), vim.log.levels.ERROR)
  end
end

---Move cursor to the right, crossing into Herdr pane if at Neovim edge.
---@param opts table|nil
function M.move_cursor_right(opts)
  resize_mod.is_resizing = false
  local ok, err = pcall(nav.move_cursor, 'right', opts)
  if not ok then
    vim.notify('herdr-splits: move_cursor_right failed: ' .. tostring(err), vim.log.levels.ERROR)
  end
end

---Append a filetype to ignored_filetypes at runtime (de-duped).
---@param name string Filetype name, e.g. 'fugitive'
---@return boolean true if added, false if already present
function M.add_ignored_filetype(name)
  for _, v in ipairs(config.ignored_filetypes) do
    if v == name then
      return false
    end
  end
  table.insert(config.ignored_filetypes, name)
  return true
end

---Append a buftype to ignored_buftypes at runtime (de-duped).
---@param name string Buffer type, e.g. 'help'
---@return boolean true if added, false if already present
function M.add_ignored_buftype(name)
  for _, v in ipairs(config.ignored_buftypes) do
    if v == name then
      return false
    end
  end
  table.insert(config.ignored_buftypes, name)
  return true
end

return M