summary refs log tree commit diff
path: root/bin/latexmk
diff options
context:
space:
mode:
Diffstat (limited to 'bin/latexmk')
-rwxr-xr-xbin/latexmk22
1 files changed, 18 insertions, 4 deletions
diff --git a/bin/latexmk b/bin/latexmk
index 520fe20..966e058 100755
--- a/bin/latexmk
+++ b/bin/latexmk
@@ -7,11 +7,13 @@ ENGINE=${ENGINE:-tectonic}
 ENGINE_ARGS=${ENGINE_ARGS:-}
 VIEWER=${VIEWER:-zathura}
 
+pidfile=/tmp/latexmk.pid
+once=
 reexec=
 nocolor=
 while getopts 1Rnh opt; do
 	case "$opt" in
-		1) entr_args='-z' ;;
+		1) once=1 ;;
 		R) reexec=1 ;;
 		n) nocolor=1 ;;
 		h | ?)
@@ -26,6 +28,8 @@ while getopts 1Rnh opt; do
 done
 shift $((OPTIND - 1))
 
+[ -n "${once}" ] && entr_args='-z'
+
 # http://stackoverflow.com/a/18443300/441757
 _realpath() {
 	OURPWD=$PWD
@@ -52,6 +56,7 @@ if [ -n "${reexec}" ]; then
 
 	# compile the document
 	log "running ${ENGINE}"
+	# shellcheck disable=SC2086
 	${ENGINE} ${ENGINE_ARGS} "$1"
 
 	if ! pgrep -fx "${VIEWER} $pdf" >/dev/null; then
@@ -63,9 +68,18 @@ if [ -n "${reexec}" ]; then
 	exit 0
 fi
 
+if [ -z "${once}" ]; then
+	# make sure we only run once
+	if [ -f "${pidfile}" ]; then
+		kill -TERM "$(cat "${pidfile}")" || rm "${pidfile}"
+	fi
+	printf "%d" "$$" >${pidfile}
+	trap '{ rm "${pidfile}"; }' EXIT
+fi
+
 if [ -z "$1" ]; then
-	ls *.tex
+	ls ./*.tex
 else
-	[ -f "$1" ] && printf "$1\n"
-	ls *.tex
+	[ -f "$1" ] && printf "%s\n" "$1"
+	ls ./*.tex
 fi | entr ${entr_args} -ns 'latexmk -R $0'