Skip to content

Commit

Permalink
feats: Option to open in split + CLI win parameter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lnk3 committed Mar 30, 2023
1 parent 8327f2a commit 06dc810
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 32 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ The script comes with the following defaults:
height = 100,
width_ratio = 0.7, -- maximum width of the Glow window compared to the nvim window size (overrides `width`)
height_ratio = 0.7,
in_place = false,
default_type = "preview|keep|split", -- Default behaviour of output window
split_dir = "split|vsplit", -- default split direction
}
```

Expand All @@ -81,15 +82,29 @@ require('glow').setup({
})
```

### Window types

When you glow on a markdown buffer you can choose one of three possible window "options":

- `preview`: open output in preview window
- `keep`: open output in same window as input buffer
- `split`: open window in a split (vertical or horizontal based on `opts.split_dir`


## Usage

### Preview file

```
:Glow [path-to-md-file]
:Glow [path-to-md-file] [window_type]
:Glow [window_type] [path-to-md-file]
:Glow split -> render current file in split
:Glow keep % -> render current file in current window
:Glow % preview -> render current file in preview window
```

### Preview current buffer
### Preview current buffer with default window type

```
:Glow
Expand All @@ -101,4 +116,4 @@ require('glow').setup({
:Glow!
```

You can also close the floating window or go back to the initial buffer using `q` or `<Esc>` keys
You can also close the floating window / split or go back to the initial buffer using `q` or `<Esc>` keys
70 changes: 44 additions & 26 deletions lua/glow.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local win, buf, tmpfile
local tmpfile
local in_place_state = {}
local types = { "preview", "keep", "split" }
local job = {}
local glow = {}

Expand All @@ -13,7 +14,8 @@ glow.config = {
pager = false,
width = 100,
height = 100,
in_place = false,
default_type = types[1], -- one of preview, keep, split
split_dir = "vsplit"
}

local function cleanup()
Expand Down Expand Up @@ -53,15 +55,15 @@ end
local function close_window()
stop_job()
cleanup()
if not glow.config.in_place then
vim.api.nvim_win_close(win, true)

local to_close_win = vim.fn.win_getid()
local managed = in_place_state[to_close_win]
in_place_state[to_close_win] = nil

if managed == true then -- Means it was a split window or a preview window so close it
vim.api.nvim_win_close(to_close_win, true)
else
local to_close_win = vim.fn.win_getid()
local managed = in_place_state[to_close_win]
if managed then
vim.api.nvim_win_set_buf(to_close_win, managed[1])
in_place_state[to_close_win] = nil
end
vim.api.nvim_win_set_buf(to_close_win, managed[1]) -- restore previous buffer don't close
end
end

Expand All @@ -76,7 +78,7 @@ local function tmp_file()
return tmp
end

local function open_window(cmd_args)
local function open_window(cmd_args, type)
local width = vim.o.columns
local height = vim.o.lines
local height_ratio = glow.config.height_ratio or 0.7
Expand Down Expand Up @@ -109,18 +111,28 @@ local function open_window(cmd_args)
}

-- create preview buffer and set local options
buf = vim.api.nvim_create_buf(false, true)

if not glow.config.in_place then
win = vim.api.nvim_open_win(buf, true, win_opts)
vim.api.nvim_win_set_option(win, "winblend", 0)
local buf = vim.api.nvim_create_buf(false, true)

if type == "split" then
vim.cmd(glow.config.split_dir)
local split_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(split_win, buf)
in_place_state[split_win] = true
vim.api.nvim_win_set_option(split_win, "winblend", 0)

elseif type == "keep" then
local orig_win = vim.api.nvim_get_current_win()
-- local buf_in_place = vim.api.nvim_get_current_buf()
in_place_state[orig_win] = { vim.fn.bufnr(), buf }
vim.api.nvim_win_set_buf(orig_win, buf)

elseif type == "preview" then
local new_win = vim.api.nvim_open_win(buf, true, win_opts)
in_place_state[new_win] = true
vim.api.nvim_win_set_option(new_win, "winblend", 0)
else
local win_in_place = vim.api.nvim_get_current_win()
local buf_in_place = vim.api.nvim_get_current_buf()

in_place_state[win_in_place] = { buf_in_place, buf }

vim.api.nvim_win_set_buf(win_in_place, buf)
err("Invalid type")
return
end

-- options
Expand Down Expand Up @@ -247,6 +259,13 @@ local function execute(opts)
return
end

-- Reorder arguments first is file|nil and second is preview|split|keep|nil
if vim.tbl_contains(types, opts.fargs[1]) then
local arg1 = opts.fargs[1] -- Save because line below will overwrite
opts.fargs[1] = opts.fargs[2] -- `Glow split` | `Glow split file.md` -> `nil` | `file.md`
opts.fargs[2] = arg1 -- Becomes preview|keep|split
end

local filename = opts.fargs[1]

if filename ~= nil and filename ~= "" then
Expand Down Expand Up @@ -285,7 +304,7 @@ local function execute(opts)
end

table.insert(cmd_args, file)
open_window(cmd_args)
open_window(cmd_args, opts.fargs[2] or glow.config.default_type)
end

local function install_glow(opts)
Expand Down Expand Up @@ -349,7 +368,7 @@ end
local function create_autocmds()
vim.api.nvim_create_user_command("Glow", function(opts)
glow.execute(opts)
end, { complete = "file", nargs = "?", bang = true })
end, { complete = "file", nargs = "*", bang = true })
end

glow.setup = function(params)
Expand All @@ -363,8 +382,7 @@ glow.execute = function(opts)
return
end

local current_win = vim.fn.win_getid()
if ( current_win == win and not glow.config.in_place) or in_place_state[current_win] then
if in_place_state[vim.fn.win_getid()] then
if opts.bang then
close_window()
end
Expand Down
6 changes: 4 additions & 2 deletions tests/glow/glow_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe("setup", function()
pager = false,
width = 100,
height = 100,
in_place = false,
default_type = "preview",
split_dir = "vsplit",
}
glow.setup()
assert.are.same(glow.config, expected)
Expand All @@ -28,7 +29,8 @@ describe("setup", function()
mouse = false,
width = 200,
height = 100,
in_place = true,
default_type = "keep",
split_dir = "split",
}
glow.setup(expected)
assert.are.same(glow.config, expected)
Expand Down

0 comments on commit 06dc810

Please sign in to comment.