Skip to content

Commit

Permalink
Slide mode (#31)
Browse files Browse the repository at this point in the history
* renamed server to servers

* deleted design-doc

* created type mode and renamed inventory to manager and added mode to manager

* completed command untested

* autocomplete command

* doc

* doc specified more stuff
  • Loading branch information
chomosuke authored May 12, 2024
1 parent e554f46 commit 15eaaff
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 119 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Plug 'chomosuke/typst-preview.nvim', {'tag': 'v0.3.*', do: ':TypstPreviewUpdate'
- If you followed the installation instructions, your package manager should automatically run
this for you.
- `:TypstPreview`:
- Start the preview.
- Start the preview. Optionally, the desired preview mode can be specified:
`:TypstPreview document` (default) or `:TypstPreview slide` for slide mode.
- `:TypstPreviewStop`:
- Stop the preview.
- `:TypstPreviewToggle`:
Expand Down
16 changes: 0 additions & 16 deletions design-doc.typ

This file was deleted.

9 changes: 8 additions & 1 deletion doc/typst-preview.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ CONTENTS *typst-preview*

*:TypstPreview*

Start the preview.
Start the preview. Optionally, the desired preview mode can be specified:
`:TypstPreview document` (default) or `:TypstPreview slide` for slide mode.

If a preview is already running, will open another front end for that
preview.

*:TypstPreviewStop*

Expand All @@ -38,6 +42,9 @@ CONTENTS *typst-preview*

Toggle the preview.

If the preview has been opened before, will open the preview with the same
mode as last time.

*:TypstPreviewFollowCursor*

Scroll preview as cursor moves. This is the default.
Expand Down
81 changes: 64 additions & 17 deletions lua/typst-preview/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ local events = require 'typst-preview.events'
local fetch = require 'typst-preview.fetch'
local utils = require 'typst-preview.utils'
local config = require 'typst-preview.config'
local server = require 'typst-preview.server'
local servers = require 'typst-preview.servers'

local M = {}

---Scroll all preview to cursor position.
function M.sync_with_cursor()
for _, ser in pairs(server.get_all()) do
server.sync_with_cursor(ser)
for _, ser in pairs(servers.get_all()) do
servers.sync_with_cursor(ser)
end
end

Expand All @@ -18,14 +18,25 @@ function M.create_commands()
local function preview_off()
local path = utils.get_buf_path(0)

if path ~= '' and server.remove(config.opts.get_main_file(path)) then
if path ~= '' and servers.remove(config.opts.get_main_file(path)) then
utils.print 'Preview stopped'
else
utils.print 'Preview not running'
end
end

local function preview_on()
local function get_path()
local path = utils.get_buf_path(0)
if path == '' then
utils.notify('Can not preview an unsaved buffer.', vim.log.levels.ERROR)
return nil
else
return config.opts.get_main_file(path)
end
end

---@param mode mode?
local function preview_on(mode)
-- check if binaries are available and tell them to fetch first
for _, bin in pairs(fetch.bins_to_fetch()) do
if
Expand All @@ -40,19 +51,20 @@ function M.create_commands()
end
end

local path = utils.get_buf_path(0)
if path == '' then
print 'Can not preview an unsaved buffer.'
local path = get_path()
if path == nil then
return
end

path = config.opts.get_main_file(path)
local s = server.get(path)
if s == nil then
server.init(path, function(ser)
events.listen(ser)
mode = mode or 'document'

local ser = servers.get(path)
if ser == nil or ser[mode] == nil then
servers.init(path, mode, function(s)
events.listen(s)
end)
else
local s = ser[mode]
print 'Opening another frontend'
utils.visit(s.link)
end
Expand All @@ -62,14 +74,49 @@ function M.create_commands()
fetch.fetch(nil)
end, {})

vim.api.nvim_create_user_command('TypstPreview', preview_on, {})
vim.api.nvim_create_user_command('TypstPreview', function(opts)
local mode
if #opts.fargs == 1 then
mode = opts.fargs[1]
if mode ~= 'document' and mode ~= 'slide' then
utils.notify(
'Invalid preview mode: "'
.. mode
.. '.'
.. ' Should be one of "document" and "slide"',
vim.log.levels.ERROR
)
end
else
assert(#opts.fargs == 0)
local path = get_path()
if path == nil then
return
end
local sers = servers.get(path)
if sers ~= nil then
mode = servers.get_last_mode(path)
end
end

preview_on(mode)
end, {
nargs = '?',
complete = function(_, _, _)
return { 'document', 'slide' }
end,
})
vim.api.nvim_create_user_command('TypstPreviewStop', preview_off, {})
vim.api.nvim_create_user_command('TypstPreviewToggle', function()
local path = utils.get_buf_path(0)
if path ~= '' and server.get(config.opts.get_main_file(path)) ~= nil then
local path = get_path()
if path == nil then
return
end

if servers.get(path) ~= nil then
preview_off()
else
preview_on()
preview_on(servers.get_last_mode(path))
end
end, {})

Expand Down
8 changes: 4 additions & 4 deletions lua/typst-preview/events/editor.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local server = require 'typst-preview.server'
local servers = require 'typst-preview.servers'
local utils = require 'typst-preview.utils'
local config= require 'typst-preview.config'

Expand All @@ -12,7 +12,7 @@ function M.register_autocmds(bufnr)
{
event = { 'TextChanged', 'TextChangedI', 'TextChangedP', 'InsertLeave' },
callback = function(ser, _)
server.update_memory_file(
servers.update_memory_file(
ser,
utils.get_buf_path(bufnr),
utils.get_buf_content(bufnr)
Expand All @@ -29,7 +29,7 @@ function M.register_autocmds(bufnr)
if last_line ~= line then
-- No scroll when on the same line in insert mode
last_line = line
server.sync_with_cursor(ser)
servers.sync_with_cursor(ser)
end
end,
},
Expand All @@ -41,7 +41,7 @@ function M.register_autocmds(bufnr)
event = autocmd.event,
opts = {
callback = function(ev)
for _, ser in pairs(server.get_all()) do
for _, ser in pairs(servers.get_all()) do
autocmd.callback(ser, ev)
end
end,
Expand Down
4 changes: 2 additions & 2 deletions lua/typst-preview/events/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local event_server = require 'typst-preview.events.server'
local utils = require 'typst-preview.utils'
local editor = require 'typst-preview.events.editor'
local server = require 'typst-preview.server'
local servers = require 'typst-preview.servers'

local M = {}

Expand All @@ -27,7 +27,7 @@ function M.init()
{
event = 'VimLeavePre',
opts = {
callback = server.remove_all,
callback = servers.remove_all,
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions lua/typst-preview/events/server.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local server = require 'typst-preview.server'
local servers = require 'typst-preview.servers'
local utils = require 'typst-preview.utils'

local M = {}

---Register event listener
---@param s Server
function M.add_listeners(s)
server.listen_scroll(s, function(event)
servers.listen_scroll(s, function(event)
local function editorScrollTo()
utils.debug(event.end_.row .. ' ' .. event.end_.column)
s.suppress = true
Expand Down
67 changes: 0 additions & 67 deletions lua/typst-preview/server/inventory.lua

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ local M = {}

---Spawn the server and connect to it using the websocat process
---@param path string
---@param mode mode
---@param callback fun(close: fun(), write: fun(data: string), read: fun(on_read: fun(data: string)), link: string)
---Called after server spawn completes
local function spawn(path, callback)
local function spawn(path, mode, callback)
local server_stdout = assert(vim.loop.new_pipe())
local server_stderr = assert(vim.loop.new_pipe())
local typst_preview_bin = config.opts.dependencies_bin['typst-preview']
Expand All @@ -19,6 +20,8 @@ local function spawn(path, callback)
'--partial-rendering',
'--invert-colors',
config.opts.invert_colors,
'--preview-mode',
mode,
'--no-open',
'--data-plane-host',
'127.0.0.1:0',
Expand Down Expand Up @@ -141,12 +144,14 @@ end

---create a new Server
---@param path string
---@param mode mode
---@param callback fun(server: Server)
function M.new(path, callback)
spawn(path, function(close, write, read, link)
function M.new(path, mode, callback)
spawn(path, mode, function(close, write, read, link)
---@type Server
local server = {
path = path,
mode = mode,
link = link,
suppress = false,
close = close,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
local utils = require 'typst-preview.utils'
local inventory = require 'typst-preview.server.inventory'
local manager = require 'typst-preview.servers.manager'
local M = {
init = inventory.init,
get = inventory.get,
get_all = inventory.get_all,
remove = inventory.remove,
remove_all = inventory.remove_all,
get_last_mode = manager.get_last_mode,
init = manager.init,
get = manager.get,
get_all = manager.get_all,
remove = manager.remove,
remove_all = manager.remove_all,
}

---@alias mode 'document'|'slide'

---@class (exact) Server
---@field path string Unsaved buffer will not be previewable.
---@field mode mode
---@field link string
---@field suppress boolean Prevent server initiated event to trigger editor initiated events.
---@field close fun()
Expand Down
Loading

0 comments on commit 15eaaff

Please sign in to comment.