From a11c1fd6f449f2ba559eb5f6e60a021c75ffe2d2 Mon Sep 17 00:00:00 2001 From: ChasingZenith Date: Mon, 24 Apr 2023 05:54:19 +0000 Subject: [PATCH 1/9] fix inverse synctex problem Issue #15 --- lua/knap.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/knap.lua b/lua/knap.lua index 63bef82..834c77f 100644 --- a/lua/knap.lua +++ b/lua/knap.lua @@ -87,9 +87,9 @@ function buffer_init() markdowntopdfviewerrefresh = "none", texoutputext = "pdf", textopdf = "pdflatex -interaction=batchmode -halt-on-error -synctex=1 %docroot%", - textopdfviewerlaunch = "sioyek --inverse-search 'nvim --headless -es --cmd \"lua require('\"'\"'knaphelper'\"'\"').relayjump('\"'\"'%servername%'\"'\"','\"'\"'%1'\"'\"',%2,%3)\"' --new-window %outputfile%", + textopdfviewerlaunch = "sioyek --inverse-search 'nvim --headless -c \"lua require('\"'\"'knaphelper'\"'\"').relayjump('\"'\"'%servername%'\"'\"','\"'\"'%1'\"'\"',%2,%3)\"' --new-window %outputfile%", textopdfviewerrefresh = "none", - textopdfforwardjump = "sioyek --inverse-search 'nvim --headless -es --cmd \"lua require('\"'\"'knaphelper'\"'\"').relayjump('\"'\"'%servername%'\"'\"','\"'\"'%1'\"'\"',%2,%3)\"' --reuse-window --forward-search-file %srcfile% --forward-search-line %line% %outputfile%", + textopdfforwardjump = "sioyek --inverse-search 'nvim --headless -c \"lua require('\"'\"'knaphelper'\"'\"').relayjump('\"'\"'%servername%'\"'\"','\"'\"'%1'\"'\"',%2,%3)\"' --reuse-window --forward-search-file %srcfile% --forward-search-line %line% %outputfile%", textopdfshorterror = "A=%outputfile% ; LOGFILE=\"${A%.pdf}.log\" ; rubber-info \"$LOGFILE\" 2>&1 | head -n 1", delay = 250 } From 6a1658116fd1190c81ab41c1ac7401b234958d0b Mon Sep 17 00:00:00 2001 From: ChasingZenith Date: Mon, 6 Nov 2023 22:26:52 +0800 Subject: [PATCH 2/9] jump to or open a buffer when doing jump to code; and focus to the neovim window using xdotool when jump to source --- lua/knap.lua | 61 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/lua/knap.lua b/lua/knap.lua index 834c77f..e695952 100644 --- a/lua/knap.lua +++ b/lua/knap.lua @@ -304,22 +304,57 @@ function is_running(pid) return (running == 0) end +local get_window_id, focus_window + + +-- +function get_window_id() + if (vim.b.xwindowid == nil) then + vim.b.xwindowid=os.getenv("WINDOWID") + end + if (vim.b.xwindowid == nil) then + local file = io.popen("xdotool selectwindow") + if (file ~= nil) then + vim.b.xwindowid = file:read("a") + file:close() + print("click on the vim window") + else + vim.b.xwindowid = -1 + end + end + return vim.b.xwindowid +end + +function focus_window() + if (get_window_id() ~= -1) then + os.execute('xdotool windowactivate ' .. get_window_id()) + end +end + -- move the cursor to a location if the file requested is the current -- one, or else just report where it should go function jump(filename,line,column) - -- compare name of destination with buffer name - local jumpbn = basename(filename) - local bufbn = basename(api.nvim_buf_get_name(0)) - if (jumpbn == bufbn) then - -- if they match then move cursor - api.nvim_win_set_cursor(0,{line,column}) - print('jumping to line ' .. tostring(line) .. ' col ' .. - tostring(column)) - else - -- if not, just report where the destination is - print('jump spot at line ' .. tostring(line) .. ' col ' .. - tostring(column) .. ' in ' .. filename) - end + local bufnr = vim.fn.bufnr(filename) + + -- switch buffer if opened + if (bufnr == -1) then + -- open file file if necessary + if (not vim.fn.filereadable(filename)) then + print('W: jump spot at line ' .. tostring(line) .. ' col ' .. + tostring(column) .. ' in ' .. filename) + return -- early + end + api.nvim_command("edit " .. vim.fn.fnameescape(filename)) + bufnr = vim.fn.bufnr(filename) + print('jump to line ' .. tostring(line) .. ' in ' .. filename) + end + + api.nvim_set_current_buf(bufnr) + api.nvim_win_set_cursor(0,{line,column}) + vim.cmd.normal('z.') + focus_window() + -- print('jumping to line ' .. tostring(line) .. ' col ' .. + -- tostring(column)) end -- run the specified command to open the viewing application From e162124f31c33e50423fcbad8884ccb2721c7a71 Mon Sep 17 00:00:00 2001 From: tangsquirrel Date: Tue, 7 Nov 2023 00:48:41 +0800 Subject: [PATCH 3/9] add wayland support fore reverse jump --- lua/knap.lua | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/lua/knap.lua b/lua/knap.lua index e695952..7f42346 100644 --- a/lua/knap.lua +++ b/lua/knap.lua @@ -304,30 +304,47 @@ function is_running(pid) return (running == 0) end -local get_window_id, focus_window +local get_window_id_x11, focus_window -- -function get_window_id() +function get_window_id_x11() if (vim.b.xwindowid == nil) then - vim.b.xwindowid=os.getenv("WINDOWID") + vim.b.xwindowid=os.getenv("WINDOWID") -- way1 to get windowid + end + if vim.fn.executable('xdotool') ~= 1 then + vim.b.xwindowid = -1 -- Can't find xdotool. Way2 can't be done + else + print("You are using X11. Can't find xdotool") end if (vim.b.xwindowid == nil) then - local file = io.popen("xdotool selectwindow") - if (file ~= nil) then - vim.b.xwindowid = file:read("a") - file:close() - print("click on the vim window") + local out = io.popen("xdotool selectwindow") -- way2 to get windowid + if (out ~= nil) then + vim.b.xwindowid = out:read("a") + out:close() + print("click on the vim window") -- to get window id else - vim.b.xwindowid = -1 + vim.b.xwindowid = -1 -- if both way can't find windowid + print("can't find window id. x11") end end + -- when vim.b.xwindowid == -1 it means that we can't find windowid. return vim.b.xwindowid end function focus_window() - if (get_window_id() ~= -1) then - os.execute('xdotool windowactivate ' .. get_window_id()) + local xdg_session_type = os.getenv("XDG_SESSION_TYPE") + if (xdg_session_type == "x11") then + local window_id_x11 = get_window_id_x11() + if (window_id_x11 ~= -1) then + os.execute('xdotool windowactivate ' .. window_id_x11) + end + elseif (xdg_session_type == "wayland") then + -- https://github.com/lucaswerkmeister/activate-window-by-title + -- This way is not very perfect but work + -- I don't know how to get something similar to windows id like in X. + -- So I only activate window which has suffix '.tex' + os.execute("busctl --user call org.gnome.Shell /de/lucaswerkmeister/ActivateWindowByTitle de.lucaswerkmeister.ActivateWindowByTitle activateBySuffix s '.tex'") end end From 3604f6ad6ea8251e6b24c97f198b5738dd9f80aa Mon Sep 17 00:00:00 2001 From: tangsquirrel Date: Tue, 7 Nov 2023 00:56:11 +0800 Subject: [PATCH 4/9] add descripition in README for activating/raising window when doing reverse jump --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d99a4bb..995b412 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,8 @@ nvim --headless -es --cmd "lua require('knaphelper').relayjump('%servername%','% If this command can be configured to be used by the viewer in a viewerlaunch or viewerrefresh action, `%servername%` may be passed as part of the command using the substitution method mentioned above; the values of `%{filename}%`, `%{line}%` and `%{column}%` must be provided by the viewer or a helper script. Note also that if one uses `'all'` for `servername`, it will relay the command to all instances of neovim whose RPC server socket it can find, which may be useful if there is no easy way to configure the viewer launching the headless instance to use a specific server. +For reverse jump, knap can activate/raise the window to the front. For X system, `xdotool` needs to be installed. For Wayland, I only test on Gnome. In Gnome shell on wayland, one needs to install the gnome shell extension [Activate Window By Title](https://github.com/lucaswerkmeister/activate-window-by-title) and enable it. The solution for Gnome on wayland is not perfect since it is implemented by searching for window that has a name with suffix `.tex`. + ## Delay Setting / Speed Tuning Subsequent re-processing of the input file and refreshing the viewer is triggered by changes to the buffer being edited in neovim. Typically, one wishes to finish what one is typing before seeing the result, so there is a short delay between any given change and the start of the processing, and the delay timer is reset with each buffer change. This means that one must at least briefly pause editing before re-processing will start. From 26f9081cc6a59cce25128bbe891bc17561c7b061 Mon Sep 17 00:00:00 2001 From: tangsquirrel Date: Tue, 7 Nov 2023 07:45:15 +0800 Subject: [PATCH 5/9] pass test on wayland --- lua/knap.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lua/knap.lua b/lua/knap.lua index 7f42346..b3933ce 100644 --- a/lua/knap.lua +++ b/lua/knap.lua @@ -312,12 +312,7 @@ function get_window_id_x11() if (vim.b.xwindowid == nil) then vim.b.xwindowid=os.getenv("WINDOWID") -- way1 to get windowid end - if vim.fn.executable('xdotool') ~= 1 then - vim.b.xwindowid = -1 -- Can't find xdotool. Way2 can't be done - else - print("You are using X11. Can't find xdotool") - end - if (vim.b.xwindowid == nil) then + if (vim.b.xwindowid == nil and vim.fn.executable('xdotool') == 1 ) then local out = io.popen("xdotool selectwindow") -- way2 to get windowid if (out ~= nil) then vim.b.xwindowid = out:read("a") @@ -327,6 +322,9 @@ function get_window_id_x11() vim.b.xwindowid = -1 -- if both way can't find windowid print("can't find window id. x11") end + else + vim.b.xwindowid = -1 -- Can't find xdotool. Way2 can't be done + print("You are using X11, but Can't find xdotool") end -- when vim.b.xwindowid == -1 it means that we can't find windowid. return vim.b.xwindowid @@ -344,7 +342,7 @@ function focus_window() -- This way is not very perfect but work -- I don't know how to get something similar to windows id like in X. -- So I only activate window which has suffix '.tex' - os.execute("busctl --user call org.gnome.Shell /de/lucaswerkmeister/ActivateWindowByTitle de.lucaswerkmeister.ActivateWindowByTitle activateBySuffix s '.tex'") + os.execute("busctl --user call org.gnome.Shell /de/lucaswerkmeister/ActivateWindowByTitle de.lucaswerkmeister.ActivateWindowByTitle activateBySuffix s '.tex' > /dev/null") end end From 7c10ad4b4a6cd7b0fe0aadd684f2779030e82ddc Mon Sep 17 00:00:00 2001 From: ChasingZenith Date: Tue, 7 Nov 2023 08:01:12 +0800 Subject: [PATCH 6/9] tested on X --- lua/knap.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lua/knap.lua b/lua/knap.lua index b3933ce..172ec1c 100644 --- a/lua/knap.lua +++ b/lua/knap.lua @@ -312,19 +312,22 @@ function get_window_id_x11() if (vim.b.xwindowid == nil) then vim.b.xwindowid=os.getenv("WINDOWID") -- way1 to get windowid end - if (vim.b.xwindowid == nil and vim.fn.executable('xdotool') == 1 ) then + if vim.b.xwindowid ~= nil then + return vim.b.xwindowid + end + if (vim.fn.executable('xdotool') == 1 ) then + print("Please click on the vim window. Using xdotool selectwindow to get window ID") -- to get window id local out = io.popen("xdotool selectwindow") -- way2 to get windowid if (out ~= nil) then vim.b.xwindowid = out:read("a") out:close() - print("click on the vim window") -- to get window id else vim.b.xwindowid = -1 -- if both way can't find windowid - print("can't find window id. x11") + print("You are using X11. But can't find window id even though xdotool is executable.") end else vim.b.xwindowid = -1 -- Can't find xdotool. Way2 can't be done - print("You are using X11, but Can't find xdotool") + print("You are using X11. But xdotool is not found") end -- when vim.b.xwindowid == -1 it means that we can't find windowid. return vim.b.xwindowid @@ -334,6 +337,7 @@ function focus_window() local xdg_session_type = os.getenv("XDG_SESSION_TYPE") if (xdg_session_type == "x11") then local window_id_x11 = get_window_id_x11() + -- print(window_id_x11) if (window_id_x11 ~= -1) then os.execute('xdotool windowactivate ' .. window_id_x11) end From 6f2634596ac2331e4c62d527bf717d1c63e8344b Mon Sep 17 00:00:00 2001 From: tangsquirrel Date: Sat, 25 Nov 2023 10:27:10 +0100 Subject: [PATCH 7/9] Fix new-window does not appear --- lua/knap.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/knap.lua b/lua/knap.lua index 172ec1c..98f99f9 100644 --- a/lua/knap.lua +++ b/lua/knap.lua @@ -87,7 +87,7 @@ function buffer_init() markdowntopdfviewerrefresh = "none", texoutputext = "pdf", textopdf = "pdflatex -interaction=batchmode -halt-on-error -synctex=1 %docroot%", - textopdfviewerlaunch = "sioyek --inverse-search 'nvim --headless -c \"lua require('\"'\"'knaphelper'\"'\"').relayjump('\"'\"'%servername%'\"'\"','\"'\"'%1'\"'\"',%2,%3)\"' --new-window %outputfile%", + textopdfviewerlaunch = "sioyek %outputfile% --new-window --inverse-search 'nvim --headless -c \"lua require('\"'\"'knaphelper'\"'\"').relayjump('\"'\"'%servername%'\"'\"','\"'\"'%1'\"'\"',%2,%3)\"'", textopdfviewerrefresh = "none", textopdfforwardjump = "sioyek --inverse-search 'nvim --headless -c \"lua require('\"'\"'knaphelper'\"'\"').relayjump('\"'\"'%servername%'\"'\"','\"'\"'%1'\"'\"',%2,%3)\"' --reuse-window --forward-search-file %srcfile% --forward-search-line %line% %outputfile%", textopdfshorterror = "A=%outputfile% ; LOGFILE=\"${A%.pdf}.log\" ; rubber-info \"$LOGFILE\" 2>&1 | head -n 1", From 8cb926b845cd21bdf5d0bcd3ddd975ffcadd1de4 Mon Sep 17 00:00:00 2001 From: tangsquirrel Date: Sat, 25 Nov 2023 10:29:24 +0100 Subject: [PATCH 8/9] Wayland window name matching pattern change, remove dot. --- lua/knap.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/knap.lua b/lua/knap.lua index 98f99f9..92b2cde 100644 --- a/lua/knap.lua +++ b/lua/knap.lua @@ -346,7 +346,7 @@ function focus_window() -- This way is not very perfect but work -- I don't know how to get something similar to windows id like in X. -- So I only activate window which has suffix '.tex' - os.execute("busctl --user call org.gnome.Shell /de/lucaswerkmeister/ActivateWindowByTitle de.lucaswerkmeister.ActivateWindowByTitle activateBySuffix s '.tex' > /dev/null") + os.execute("busctl --user call org.gnome.Shell /de/lucaswerkmeister/ActivateWindowByTitle de.lucaswerkmeister.ActivateWindowByTitle activateBySuffix s 'tex' > /dev/null") end end From c9e8f593aaee0906788ece85723c0d8415f2db5c Mon Sep 17 00:00:00 2001 From: tangsquirrel Date: Sat, 2 Nov 2024 15:07:25 +0100 Subject: [PATCH 9/9] raise window in KDE --- lua/knap.lua | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lua/knap.lua b/lua/knap.lua index e8d330b..f2639b0 100644 --- a/lua/knap.lua +++ b/lua/knap.lua @@ -381,29 +381,31 @@ function focus_window() os.execute('xdotool windowactivate ' .. window_id_x11) end elseif (xdg_session_type == "wayland") then - -- https://github.com/lucaswerkmeister/activate-window-by-title - -- This way is not very perfect but work - -- I don't know how to get something similar to windows id like in X. - -- So I only activate window which has suffix '.tex' - os.execute("busctl --user call org.gnome.Shell /de/lucaswerkmeister/ActivateWindowByTitle de.lucaswerkmeister.ActivateWindowByTitle activateBySuffix s 'tex' > /dev/null") + local home = os.getenv("HOME") + os.execute(home .. "/.local/bin/raise_neovim_tex.sh") + -- elseif (xdg_session_type == "wayland") then + -- -- https://github.com/lucaswerkmeister/activate-window-by-title + -- -- This way is not very perfect but work + -- -- I don't know how to get something similar to windows id like in X. + -- -- So I only activate window which has suffix '.tex' + -- os.execute("busctl --user call org.gnome.Shell /de/lucaswerkmeister/ActivateWindowByTitle de.lucaswerkmeister.ActivateWindowByTitle activateBySuffix s 'tex' > /dev/null") end end -- move the cursor to a location if the file requested is the current -- one, or else just report where it should go function jump(filename,line,column) + filename = vim.fn.fnameescape(filename) local bufnr = vim.fn.bufnr(filename) - -- switch buffer if opened - if (bufnr == -1) then - -- open file file if necessary - if (not vim.fn.filereadable(filename)) then + if (bufnr == -1) then -- switch buffer if opened + if (not vim.fn.filereadable(filename)) then -- open file file if necessary print('W: jump spot at line ' .. tostring(line) .. ' col ' .. tostring(column) .. ' in ' .. filename) return -- early end - api.nvim_command("edit " .. vim.fn.fnameescape(filename)) - bufnr = vim.fn.bufnr(filename) + -- api.nvim_command("edit " .. filename) + bufnr = vim.fn.bufadd(filename) -- need test print('jump to line ' .. tostring(line) .. ' in ' .. filename) end @@ -411,8 +413,8 @@ function jump(filename,line,column) api.nvim_win_set_cursor(0,{line,column}) vim.cmd.normal('z.') focus_window() - -- print('jumping to line ' .. tostring(line) .. ' col ' .. - -- tostring(column)) + print('jumping to line ' .. tostring(line) .. ' col ' .. + tostring(column)) end function get_pid_viewer(lcmd)