AutoYADM commit: 2025-05-18 22:37:50
This commit is contained in:
parent
1affe9ad60
commit
a710ff0668
2 changed files with 63 additions and 20 deletions
|
@ -94,3 +94,9 @@ map -docstring 'case insensitive backward extend-search' global user '<a-?>' <a-
|
|||
map -docstring 'Select to file end' global user N "Gj<a-L>L"
|
||||
map -docstring 'Extend to file end' global user n "gj<a-l>l"
|
||||
|
||||
define-command typst %{
|
||||
nop %sh{
|
||||
{"$kak_config/scripts/kak-typ-zathura.fish" -k -w "$kak_buffile" "$kak_client_pid"} > /dev/null 2>&1 < /dev/null &
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,13 +3,14 @@
|
|||
# hx-typ-zathura: easily preview your typst documents from Helix!
|
||||
# This script will automatically find a pdf that matches your
|
||||
# current Typst document and attempt to open it with Zathura.
|
||||
# Optionally, it will quit Zathura when you close Helix.
|
||||
# Optionally, it will quit Zathura when you close Helix, and
|
||||
# uses typst watch to continuously compile the file.
|
||||
# Run this script with --help for usage info!
|
||||
|
||||
# Author: Daniel Fichtinger <daniel@ficd.ca
|
||||
# License: MIT
|
||||
|
||||
argparse q/quiet k/kill-on-exit h/help p/process -- $argv
|
||||
argparse q/quiet k/kill-on-exit w/watch h/help -- $argv
|
||||
|
||||
if test (count $argv) -eq 0; or set -q _flag_h
|
||||
echo "Helper script for opening Typst files from Helix in Zathura."\n
|
||||
|
@ -21,7 +22,11 @@ if test (count $argv) -eq 0; or set -q _flag_h
|
|||
echo "Options:"
|
||||
echo "-q/--quiet: Don't \`echo\` on caught errors, return 1 instead."
|
||||
echo "-k/--kill-on-exit: Kill Zathura when parent Helix process exits."
|
||||
echo "-w/--watch: live preview mode"
|
||||
echo "-h/--help: print this screen"\n
|
||||
echo "Note:"
|
||||
echo "To avoid spawning a large number of typst processes,"
|
||||
echo "--watch is only effective if --kill-on-exit is also set."\n
|
||||
echo 'Author: Daniel Fichtinger <daniel@ficd.ca>'
|
||||
echo 'License: MIT'
|
||||
return 0
|
||||
|
@ -43,40 +48,74 @@ function qecho
|
|||
end
|
||||
end
|
||||
|
||||
# absolute path of %{buffer_name} file
|
||||
set -g src_path (path resolve $argv[1])
|
||||
|
||||
# check if the user asked to kill zathura on helix exit
|
||||
if set -q _flag_k
|
||||
set kill_parent
|
||||
set client_id $_flag_p
|
||||
# or to watch the pdf
|
||||
if set -q _flag_k || set -q _flag_w
|
||||
if set -q _flag_k
|
||||
set kill_parent
|
||||
end
|
||||
if set -q _flag_w
|
||||
set typst_watch
|
||||
end
|
||||
# traverse up process tree to find caller Helix PID
|
||||
# We use this PID to kill zathura if Helix exits first!
|
||||
# we only need to define this function
|
||||
# inside this scope
|
||||
function find_parent_process
|
||||
echo "$client_id"
|
||||
function find_parent_process -a target
|
||||
# initialize current_pid as this shell's pid
|
||||
set -f current_pid $fish_pid
|
||||
|
||||
# Stop when we reach init (PID 1)
|
||||
while test $current_pid -ne 1
|
||||
# parent of current_pid
|
||||
set parent (ps -o ppid= -p $current_pid | string trim)
|
||||
# get parent's command name
|
||||
set cmd (ps -o comm= -p $parent | string trim)
|
||||
|
||||
# if the cmd matches our target command we return its pid
|
||||
if string match -q -- $target $cmd
|
||||
echo "$parent"
|
||||
return 0
|
||||
end
|
||||
|
||||
set -f current_pid $parent
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
set parent_pid (find_parent_process hx)
|
||||
set parent_pid $argv[2]
|
||||
if test -z "$parent_pid"
|
||||
qecho "Couldn't find parent hx process!"
|
||||
qecho "Couldn't find parent kak process!"
|
||||
return $ret
|
||||
end
|
||||
end
|
||||
|
||||
# opens zathura, optionally watching for helix closing
|
||||
function zopen --wraps zathura
|
||||
# this should be set if the user asked to watch
|
||||
set -f pdf_path $argv[1]
|
||||
# this should be set if the user asked to kill
|
||||
if set -q kill_parent
|
||||
# create background sub-process
|
||||
# otherwise helix will hang
|
||||
begin
|
||||
zathura "$argv[1]" &>/dev/null &
|
||||
zathura "$pdf_path" &>/dev/null &
|
||||
set zathura_pid $last_pid
|
||||
if set -q typst_watch
|
||||
typst watch "$src_path" "$pdf_path" &>/dev/null &
|
||||
end
|
||||
set typst_pid $last_pid
|
||||
waitpid -c 1 "$parent_pid" "$zathura_pid"
|
||||
kill $zathura_pid &>/dev/null
|
||||
if set -q typst_watch
|
||||
kill $typst_pid &>/dev/null
|
||||
end
|
||||
end &
|
||||
else
|
||||
# user didn't ask for watch, so open normally
|
||||
zathura "$argv[1]" &>/dev/null &
|
||||
# user didn't ask for kill, so open normally
|
||||
zathura "$pdf_path" &>/dev/null &
|
||||
end
|
||||
true
|
||||
end
|
||||
|
@ -93,19 +132,17 @@ function find_pdf
|
|||
end
|
||||
end
|
||||
|
||||
# absolute path of %{buffer_name} file
|
||||
set -l src (path resolve $argv[1])
|
||||
# echo $src
|
||||
# echo $src_path
|
||||
# return 0
|
||||
# exit if not a typst file
|
||||
if not string match -q '*.typ' $src
|
||||
qecho "$(path basename $src) is not a Typst file!"
|
||||
if not string match -q '*.typ' $src_path
|
||||
qecho "$(path basename $src_path) is not a Typst file!"
|
||||
return $ret
|
||||
end
|
||||
# change abs path to pdf extension
|
||||
set -l targ (string replace --regex '\.typ$' '.pdf' $src)
|
||||
set -l targ (string replace --regex '\.typ$' '.pdf' $src_path)
|
||||
# get pdf target's base name
|
||||
set -l base (path basename --no-extension $src).pdf
|
||||
set -l base (path basename --no-extension $src_path).pdf
|
||||
|
||||
# if a suitable pdf exists in the same dir, open it
|
||||
if test -f "$targ"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue