Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open Glow output in the same window as input buffer or split #120

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

lnc3l0t
Copy link

@lnc3l0t lnc3l0t commented Mar 30, 2023

The branch has two commits with detailed explanations in the commit message.

Commit 8327f2a adds a global configuration option to allow for the output to be rendered in_place. When in a markdown buffer :Glow<CR> renders the output in the same window as the buffer, and q, <Esc> and :Glow!<cr> restore the original buffer in the window.

Commit 06dc810 goes a step further than just adding the "split" functionality (which similarly to before allows the output to be in a new split. It also make the usercommand Glow accept a second argument (identified as type for window type) that overrides the config option and opens the output in the specified window (current, split, preview).

Both commit allow to have multiple markdown buffers be rendered and then closed which wasn't possible before because issuing :Glow<CR> twice would override the global win variable and closing it wouldn't be possible, try:

  • open fileA.md -> :Glow<cr> -> <C-w><C-w> -> open fileB.md -> :Glow<cr> -> q -> fileB output closes
  • now <c-w><c-w> until back on the initial preview window and press q the error pops up
E5108: Error executing lua: .../AppData/Local/nvim-data/lazy/glow.nvim/lua/glow.lua:54: Invalid window id: 1013
stack traceback:
        [C]: in function 'nvim_win_close'
        .../AppData/Local/nvim-data/lazy/glow.nvim/lua/glow.lua:54: in function <.../AppData/Local/nvim-data/lazy/glow.nvim/lua/glow.lua:51>

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.
@drjaska
Copy link

drjaska commented Apr 13, 2023

If I open a render with :Glow keep, am I supposed to return to be able to return to the old buffer? Is app exiting supposed to be mandatory? I'm able to return to it with :e filename.md but now I can't open a new render anymore.

Is the preview window supposed to be able to get the background buffer with :bNext or :bprevious? I can't cycle between them but I can re-render just fine after closing the preview window.

@lnc3l0t
Copy link
Author

lnc3l0t commented Apr 28, 2023

Sorry for the late reply @drjaska.

If I open a render with :Glow keep, am I supposed to return to be able to return to the old buffer? Is app exiting supposed to be mandatory? I'm able to return to it with :e filename.md but now I can't open a new render anymore.

I don't know if I understood properly but since before my PR you can close the rendered view with q or <Esc>. The plugin sets local keymaps to the render buffer at line 111-112 (lines 144-145 in my PR are unchanged)

vim.keymap.set("n", "q", close_window, keymaps_opts)
vim.keymap.set("n", "<Esc>", close_window, keymaps_opts)

so perhaps you should use that instead of reopening buffer with :e filename.md.

Is the preview window supposed to be able to get the background buffer with :bNext or :bprevious? I can't cycle between them but I can re-render just fine after closing the preview window.

The render buffer is wiped from the buffer list when it is no longer displayed in a window (:h bufhidden with wipe option). If you are inside the preview window bNext/bprevious should open the input buffer in the preview window (assuming there aren't other buffers opened, or you will need to bNext/bprevious more than once), but after that you can't go back to the render buffer since it has been wiped.

I usually used preview as such: Glow preview and then just close it with q/<Esc>, or cycle back to input file with <C-w>w.

If you would like to have the original buffer in the preview window I can work on it. Maybe we can have a keymap that in every render windows toggles to the original markdown file. I'll see what I can do, I think it would actually be a different feature so maybe a future PR.

Hope I was clear

There are now 3 main possible window configuration (`types` table):
- `preview`: the default one, opens a preview window, as it was until commit
  ellisonleao/glow.nvim@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.
In every type of window (keep, split, preview) you can now press P to toggle between the input and output buffers. The plugin doesn't wipe original buffer local keymaps and restores them when closing Glow
@lnc3l0t
Copy link
Author

lnc3l0t commented May 5, 2023

Since 1c35e75 you can press p in every Glow window (split, keep, preview) and it will toggle between the Glow output and a buffer which is a non modifiable copy of the original buffer.

@drjaska check it out!

closing the window and toggling between Glow/input is now customizable
@A
Copy link

A commented Jun 5, 2023

Would be great to have this merged. I just tried @lnc3l0t branch and it works great :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants