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

Commit 8327f2a

Browse files
committed
feat: new option to show Glow in same window
The option `in_place` determines if the output from Glow has to be displayed in the same window as the input buffer or instead of in a preview window like it was before this commit. The ability to manage multiple windows with different markdown buffers was incidentally implemented. The `in_place_state` table is a map where: - a key represent a certain window - its value contains both the input buffer and the output buffer (the one created by *glow.nvim* holding the content of Glow). ```lua in_place_state = { [WIN_1] = { BUFFER_BEFORE, BUFFER_CREATED }, ... } ``` So to the steps now are: - Check if the config flag is enabled - If it is save the state to retrieve BUFFER_BEFORE when needed - Replace the buffer in WIN_1 with BUFFER_CREATED - When "closing the window" reset the BUFFER_BEFORE in the WIN_1 and set in_place_state[WIN_1] to NIL Line 346 had to be changed as well or: - `Glow|Glow!|Glow` wouldn't have been possible (e.g. use command multiple times) - `Glow|Glow` printed an error: ```lua (current_win == win and not glow.config.in_place) -- keep previous behaviour or in_place_state[current_win] -- Window is managed by Glow and needs to be considered as a `glowpreview` buffer (e.g. closed or not bothered) ``` This is because before `current_win == win` always meant we were on the preview window which now isn't the case anymore.
1 parent a3f24fd commit 8327f2a

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</p>
1010
<img src="https://img.shields.io/badge/Made%20with%20Lua-blueviolet.svg?style=for-the-badge&logo=lua" />
1111
<img src="https://img.shields.io/github/actions/workflow/status/ellisonleao/glow.nvim/default.yml?style=for-the-badge" />
12-
12+
1313
</div>
1414

1515
https://user-images.githubusercontent.com/178641/215353259-eb8688fb-5600-4b95-89a2-0f286e3b6441.mp4
@@ -60,6 +60,7 @@ 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,
6364
}
6465
```
6566

@@ -94,10 +95,10 @@ require('glow').setup({
9495
:Glow
9596
```
9697

97-
### Close window
98+
### Close window or return to input buffer
9899

99100
```
100101
:Glow!
101102
```
102103

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

lua/glow.lua

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local win, buf, tmpfile
2+
local in_place_state = {}
23
local job = {}
34
local glow = {}
45

@@ -12,6 +13,7 @@ glow.config = {
1213
pager = false,
1314
width = 100,
1415
height = 100,
16+
in_place = false,
1517
}
1618

1719
local function cleanup()
@@ -51,7 +53,16 @@ end
5153
local function close_window()
5254
stop_job()
5355
cleanup()
54-
vim.api.nvim_win_close(win, true)
56+
if not glow.config.in_place then
57+
vim.api.nvim_win_close(win, true)
58+
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
65+
end
5566
end
5667

5768
local function tmp_file()
@@ -99,10 +110,20 @@ local function open_window(cmd_args)
99110

100111
-- create preview buffer and set local options
101112
buf = vim.api.nvim_create_buf(false, true)
102-
win = vim.api.nvim_open_win(buf, true, win_opts)
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)
117+
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)
124+
end
103125

104126
-- options
105-
vim.api.nvim_win_set_option(win, "winblend", 0)
106127
vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
107128
vim.api.nvim_buf_set_option(buf, "filetype", "glowpreview")
108129

@@ -343,7 +364,7 @@ glow.execute = function(opts)
343364
end
344365

345366
local current_win = vim.fn.win_getid()
346-
if current_win == win then
367+
if ( current_win == win and not glow.config.in_place) or in_place_state[current_win] then
347368
if opts.bang then
348369
close_window()
349370
end

tests/glow/glow_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe("setup", function()
1212
pager = false,
1313
width = 100,
1414
height = 100,
15+
in_place = false,
1516
}
1617
glow.setup()
1718
assert.are.same(glow.config, expected)
@@ -27,6 +28,7 @@ describe("setup", function()
2728
mouse = false,
2829
width = 200,
2930
height = 100,
31+
in_place = true,
3032
}
3133
glow.setup(expected)
3234
assert.are.same(glow.config, expected)

0 commit comments

Comments
 (0)