Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 06dc810

Browse files
committed
feats: Option to open in split + CLI win parameter
There are now 3 main possible window configuration (`types` table): - `preview`: the default one, opens a preview window, as it was until commit a3f24fd - `keep`: keeps the same window as input buffer for the output from Glow - `split`: opens a split and puts the output there The user can configure his preferred behaviour with `glow.config.default_type` which can be "preview|keep|split". The split direction can be configured with `glow.config.split_dir` to either be "split|vsplit". The user can also override the default window type via the `Glow` user command, by providing the type: - `Glow <file>` opens <file> with `glow.config.default_type` - `Glow <type>` opens current file with <type> - `Glow <file> <type>` opens <file> with <type> - `Glow <type> <file>` opens <file> with <type> The order is not required by the user, we internally order the args to be file first and type second.
1 parent 8327f2a commit 06dc810

File tree

3 files changed

+67
-32
lines changed

3 files changed

+67
-32
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ The script comes with the following defaults:
6060
height = 100,
6161
width_ratio = 0.7, -- maximum width of the Glow window compared to the nvim window size (overrides `width`)
6262
height_ratio = 0.7,
63-
in_place = false,
63+
default_type = "preview|keep|split", -- Default behaviour of output window
64+
split_dir = "split|vsplit", -- default split direction
6465
}
6566
```
6667

@@ -81,15 +82,29 @@ require('glow').setup({
8182
})
8283
```
8384

85+
### Window types
86+
87+
When you glow on a markdown buffer you can choose one of three possible window "options":
88+
89+
- `preview`: open output in preview window
90+
- `keep`: open output in same window as input buffer
91+
- `split`: open window in a split (vertical or horizontal based on `opts.split_dir`
92+
93+
8494
## Usage
8595

8696
### Preview file
8797

8898
```
89-
:Glow [path-to-md-file]
99+
:Glow [path-to-md-file] [window_type]
100+
:Glow [window_type] [path-to-md-file]
101+
102+
:Glow split -> render current file in split
103+
:Glow keep % -> render current file in current window
104+
:Glow % preview -> render current file in preview window
90105
```
91106

92-
### Preview current buffer
107+
### Preview current buffer with default window type
93108

94109
```
95110
:Glow
@@ -101,4 +116,4 @@ require('glow').setup({
101116
:Glow!
102117
```
103118

104-
You can also close the floating window or go back to the initial buffer using `q` or `<Esc>` keys
119+
You can also close the floating window / split or go back to the initial buffer using `q` or `<Esc>` keys

lua/glow.lua

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
local win, buf, tmpfile
1+
local tmpfile
22
local in_place_state = {}
3+
local types = { "preview", "keep", "split" }
34
local job = {}
45
local glow = {}
56

@@ -13,7 +14,8 @@ glow.config = {
1314
pager = false,
1415
width = 100,
1516
height = 100,
16-
in_place = false,
17+
default_type = types[1], -- one of preview, keep, split
18+
split_dir = "vsplit"
1719
}
1820

1921
local function cleanup()
@@ -53,15 +55,15 @@ end
5355
local function close_window()
5456
stop_job()
5557
cleanup()
56-
if not glow.config.in_place then
57-
vim.api.nvim_win_close(win, true)
58+
59+
local to_close_win = vim.fn.win_getid()
60+
local managed = in_place_state[to_close_win]
61+
in_place_state[to_close_win] = nil
62+
63+
if managed == true then -- Means it was a split window or a preview window so close it
64+
vim.api.nvim_win_close(to_close_win, true)
5865
else
59-
local to_close_win = vim.fn.win_getid()
60-
local managed = in_place_state[to_close_win]
61-
if managed then
62-
vim.api.nvim_win_set_buf(to_close_win, managed[1])
63-
in_place_state[to_close_win] = nil
64-
end
66+
vim.api.nvim_win_set_buf(to_close_win, managed[1]) -- restore previous buffer don't close
6567
end
6668
end
6769

@@ -76,7 +78,7 @@ local function tmp_file()
7678
return tmp
7779
end
7880

79-
local function open_window(cmd_args)
81+
local function open_window(cmd_args, type)
8082
local width = vim.o.columns
8183
local height = vim.o.lines
8284
local height_ratio = glow.config.height_ratio or 0.7
@@ -109,18 +111,28 @@ local function open_window(cmd_args)
109111
}
110112

111113
-- create preview buffer and set local options
112-
buf = vim.api.nvim_create_buf(false, true)
113-
114-
if not glow.config.in_place then
115-
win = vim.api.nvim_open_win(buf, true, win_opts)
116-
vim.api.nvim_win_set_option(win, "winblend", 0)
114+
local buf = vim.api.nvim_create_buf(false, true)
115+
116+
if type == "split" then
117+
vim.cmd(glow.config.split_dir)
118+
local split_win = vim.api.nvim_get_current_win()
119+
vim.api.nvim_win_set_buf(split_win, buf)
120+
in_place_state[split_win] = true
121+
vim.api.nvim_win_set_option(split_win, "winblend", 0)
122+
123+
elseif type == "keep" then
124+
local orig_win = vim.api.nvim_get_current_win()
125+
-- local buf_in_place = vim.api.nvim_get_current_buf()
126+
in_place_state[orig_win] = { vim.fn.bufnr(), buf }
127+
vim.api.nvim_win_set_buf(orig_win, buf)
128+
129+
elseif type == "preview" then
130+
local new_win = vim.api.nvim_open_win(buf, true, win_opts)
131+
in_place_state[new_win] = true
132+
vim.api.nvim_win_set_option(new_win, "winblend", 0)
117133
else
118-
local win_in_place = vim.api.nvim_get_current_win()
119-
local buf_in_place = vim.api.nvim_get_current_buf()
120-
121-
in_place_state[win_in_place] = { buf_in_place, buf }
122-
123-
vim.api.nvim_win_set_buf(win_in_place, buf)
134+
err("Invalid type")
135+
return
124136
end
125137

126138
-- options
@@ -247,6 +259,13 @@ local function execute(opts)
247259
return
248260
end
249261

262+
-- Reorder arguments first is file|nil and second is preview|split|keep|nil
263+
if vim.tbl_contains(types, opts.fargs[1]) then
264+
local arg1 = opts.fargs[1] -- Save because line below will overwrite
265+
opts.fargs[1] = opts.fargs[2] -- `Glow split` | `Glow split file.md` -> `nil` | `file.md`
266+
opts.fargs[2] = arg1 -- Becomes preview|keep|split
267+
end
268+
250269
local filename = opts.fargs[1]
251270

252271
if filename ~= nil and filename ~= "" then
@@ -285,7 +304,7 @@ local function execute(opts)
285304
end
286305

287306
table.insert(cmd_args, file)
288-
open_window(cmd_args)
307+
open_window(cmd_args, opts.fargs[2] or glow.config.default_type)
289308
end
290309

291310
local function install_glow(opts)
@@ -349,7 +368,7 @@ end
349368
local function create_autocmds()
350369
vim.api.nvim_create_user_command("Glow", function(opts)
351370
glow.execute(opts)
352-
end, { complete = "file", nargs = "?", bang = true })
371+
end, { complete = "file", nargs = "*", bang = true })
353372
end
354373

355374
glow.setup = function(params)
@@ -363,8 +382,7 @@ glow.execute = function(opts)
363382
return
364383
end
365384

366-
local current_win = vim.fn.win_getid()
367-
if ( current_win == win and not glow.config.in_place) or in_place_state[current_win] then
385+
if in_place_state[vim.fn.win_getid()] then
368386
if opts.bang then
369387
close_window()
370388
end

tests/glow/glow_spec.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ describe("setup", function()
1212
pager = false,
1313
width = 100,
1414
height = 100,
15-
in_place = false,
15+
default_type = "preview",
16+
split_dir = "vsplit",
1617
}
1718
glow.setup()
1819
assert.are.same(glow.config, expected)
@@ -28,7 +29,8 @@ describe("setup", function()
2829
mouse = false,
2930
width = 200,
3031
height = 100,
31-
in_place = true,
32+
default_type = "keep",
33+
split_dir = "split",
3234
}
3335
glow.setup(expected)
3436
assert.are.same(glow.config, expected)

0 commit comments

Comments
 (0)