Skip to content

Commit 339e403

Browse files
Add built in float option for win_split_mode
1 parent ede6170 commit 339e403

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

DOCS.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ NOTE: Make sure fast access keys do not overlap. If that happens, first entry in
7575
Should error diagnostics be shown. If you are using Neovim 0.6.0 or higher, these will be shown via `vim.diagnostic`.<br />
7676

7777
#### **win_split_mode**
78-
*type*: `string|function`<br />
78+
*type*: `string|function|table`<br />
7979
*default value*: `horizontal`<br />
8080
Available options:
81-
* horizontal - Always split horizontally
82-
* vertical - Always split vertically
83-
* auto - Determine between horizontal and vertical split depending on the current window size
81+
* `horizontal` - Always split horizontally
82+
* `vertical` - Always split vertically
83+
* `auto` - Determine between horizontal and vertical split depending on the current window size
84+
* `float` - Open in float window that has width of 70% of the screen centered
85+
* `{'float', 0.9}` - Open in float window and provide custom scale (in this case it's 90% of screen size), must be value between `0` and `1`
8486

8587
This option determines how to open agenda and capture window.<br />
8688
If none of the options above suit your needs, you can provide custom command string (see `:help <mods>`) or custom function:

lua/orgmode/capture/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ end
6868
---@param template table
6969
function Capture:open_template(template)
7070
local content = self.templates:compile(template)
71+
local winnr = vim.api.nvim_get_current_win()
7172
utils.open_window(vim.fn.tempname(), 16, config.win_split_mode)
7273
vim.cmd([[setf org]])
7374
vim.cmd([[setlocal bufhidden=wipe nobuflisted nolist noswapfile nofoldenable]])
7475
vim.api.nvim_buf_set_lines(0, 0, -1, true, content)
7576
self.templates:setup()
7677
vim.api.nvim_buf_set_var(0, 'org_template', template)
7778
vim.api.nvim_buf_set_var(0, 'org_capture', true)
79+
vim.api.nvim_buf_set_var(0, 'org_prev_window', winnr)
7880
config:setup_mappings('capture')
7981

8082
self.wipeout_autocmd_id = vim.api.nvim_create_autocmd('BufWipeout', {
@@ -320,7 +322,11 @@ function Capture:kill()
320322
vim.api.nvim_del_autocmd(self.wipeout_autocmd_id)
321323
self.wipeout_autocmd_id = nil
322324
end
323-
return vim.api.nvim_win_close(0, true)
325+
local prev_winnr = vim.api.nvim_buf_get_var(0, 'org_prev_window')
326+
vim.api.nvim_win_close(0, true)
327+
if prev_winnr and vim.api.nvim_win_is_valid(prev_winnr) then
328+
vim.api.nvim_set_current_win(prev_winnr)
329+
end
324330
end
325331

326332
return Capture

lua/orgmode/utils/init.lua

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ end
513513

514514
---@param name string
515515
---@param height number
516+
---@param split_mode string|function|table
516517
function utils.open_window(name, height, split_mode)
517518
local cmd_by_split_mode = {
518519
horizontal = string.format('%dsplit %s', height, name),
@@ -522,7 +523,10 @@ function utils.open_window(name, height, split_mode)
522523
if cmd_by_split_mode[split_mode] then
523524
vim.cmd(cmd_by_split_mode[split_mode])
524525
vim.w.org_window_split_mode = split_mode
525-
elseif split_mode == 'auto' then
526+
return
527+
end
528+
529+
if split_mode == 'auto' then
526530
local winwidth = utils.winwidth()
527531
if (winwidth / 2) >= 80 then
528532
vim.cmd(cmd_by_split_mode.vertical)
@@ -531,13 +535,47 @@ function utils.open_window(name, height, split_mode)
531535
vim.cmd(cmd_by_split_mode.horizontal)
532536
vim.w.org_window_split_mode = 'horizontal'
533537
end
534-
else
535-
if type(split_mode) == 'function' then
536-
split_mode(name)
537-
else
538-
vim.cmd(string.format('%s %s', split_mode, name))
539-
end
538+
return
539+
end
540+
541+
if type(split_mode) == 'function' then
542+
return split_mode(name)
540543
end
544+
545+
if split_mode == 'float' then
546+
return utils.open_float(name)
547+
end
548+
549+
if type(split_mode) == 'table' and split_mode[1] == 'float' then
550+
return utils.open_float(name, split_mode[2])
551+
end
552+
553+
return vim.cmd(string.format('%s %s', split_mode, name))
554+
end
555+
556+
---@param name string
557+
---@param scale? number
558+
function utils.open_float(name, scale)
559+
scale = scale or 0.7
560+
-- Make sure number is between 0 and 1
561+
scale = math.min(math.max(0, scale), 1)
562+
local bufnr = vim.api.nvim_create_buf(false, true)
563+
vim.api.nvim_buf_set_name(bufnr, name)
564+
565+
local width = math.floor((vim.o.columns * scale))
566+
local height = math.floor((vim.o.lines * scale))
567+
local row = math.floor((((vim.o.lines - height) / 2) - 1))
568+
local col = math.floor(((vim.o.columns - width) / 2))
569+
570+
vim.api.nvim_open_win(bufnr, true, {
571+
relative = 'editor',
572+
width = width,
573+
height = height,
574+
row = row,
575+
col = col,
576+
style = 'minimal',
577+
border = 'rounded',
578+
})
541579
end
542580

543581
return utils

0 commit comments

Comments
 (0)