summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-07-18 17:23:37 +0300
committerRobert Günzler <r@gnzler.io>2022-08-02 22:15:23 +0200
commitf9691d520d9b627c34e42151a61e42ca161883ae (patch)
tree4abd26adb4e0a9613a31bd34e46efc1467759ab0
parent460a4f1019d8086286d866b5388dc03503931d15 (diff)
bin/latexmk: refactor getopts, add quiet mode
-rwxr-xr-xbin/latexmk57
1 files changed, 36 insertions, 21 deletions
diff --git a/bin/latexmk b/bin/latexmk
index e56cadc..8d42d98 100755
--- a/bin/latexmk
+++ b/bin/latexmk
@@ -3,27 +3,24 @@
 set -e
 [ -n "$DEBUG" ] && set -x
 
+# save args for passing into reexec
+vargs=$*
+
 ENGINE=${ENGINE:-tectonic}
-ENGINE_ARGS=${ENGINE_ARGS:-}
 VIEWER=${VIEWER:-zathura}
-
-# check requirements
-for tool in entr realpath tectonic; do
-	if ! command -v "$tool" >/dev/null; then
-		printf "%s: %s not installed but required\n" "$(basename "$0")" "$tool" >&2
-		exit 1
-	fi
-done
-
 pidfile=/tmp/latexmk.pid
 once=
 reexec=
 nocolor=
-while getopts 1Rnhpvcf opt; do
+quiet=
+while getopts 1RV:E:qnhpvcf opt; do
 	case "$opt" in
-	1) once=1 ;;
 	R) reexec=1 ;;
+	1) once=1 ;;
 	n) nocolor=1 ;;
+	q) quiet=1 ;;
+	V) VIEWER=$OPTARG ;;
+	E) ENGINE=$OPTARG ;;
 	# noop, compat
 	p) ;;
 	v) ;;
@@ -32,8 +29,11 @@ while getopts 1Rnhpvcf opt; do
 	h | ?)
 		printf "usage: %s [-h]\n" "$(basename "$0")"
 		printf "\n"
-		printf "  -1 \t only run once\n"
-		printf "  -n \t no color\n"
+		printf "  -1         only run once\n"
+		printf "  -n         no color\n"
+		printf "  -q         be quiet\n"
+		printf "  -V VIEWER  pdf viewer (default: %s)\n" "$VIEWER"
+		printf "  -E ENGINE  latex engine (default: %s)\n" "$ENGINE"
 		printf "\n"
 		exit 2
 		;;
@@ -41,6 +41,14 @@ while getopts 1Rnhpvcf opt; do
 done
 shift $((OPTIND - 1))
 
+# check requirements
+for tool in entr realpath pgrep $ENGINE $VIEWER; do
+	if ! command -v "$tool" >/dev/null; then
+		printf "%s: %s not installed but required\n" "$(basename "$0")" "$tool" >&2
+		exit 1
+	fi
+done
+
 [ -n "${once}" ] && entr_args='-z'
 
 # http://stackoverflow.com/a/18443300/441757
@@ -58,19 +66,28 @@ _realpath() {
 }
 
 if [ -n "${reexec}" ]; then
-	pdf=$(_realpath "$(printf "%s" "$1" | sed 's/\.tex/\.pdf/')")
+	tex=$1
+	[ -z "$tex" ] || [ ! -f "$tex" ] && [ -f main.tex ] && tex=main.tex
+
+	pdf=$(_realpath "$(printf "%s" "$tex" | sed 's/\.tex/\.pdf/')")
 	log() {
+		[ -n "${quiet}" ] && return
+
 		if [ -n "${nocolor}" ]; then
 			printf "%s!==>%s %s\n" "$(tput setaf 2)" "$(tput sgr0)" "$1"
 		else
-			printf "==> %s\n" "$1"
+			printf "==> %s\n" "$tex"
 		fi
 	}
 
 	# compile the document
 	log "running ${ENGINE}"
-	# shellcheck disable=SC2086
-	${ENGINE} ${ENGINE_ARGS} "$1"
+
+	if [ -n "${quiet}" ]; then
+		${ENGINE} "$tex" >/dev/null 2>&1
+	else
+		${ENGINE} "$tex"
+	fi
 
 	if ! pgrep -fx "${VIEWER} $pdf" >/dev/null; then
 		# spawn the viewer
@@ -91,11 +108,9 @@ if [ -z "${once}" ]; then
 fi
 
 (
-	[ -f "$1" ] && printf "%s\n" "$1"
-	[ -z "$1" ] && [ -f main.tex ] && printf "main.tex\n"
 	if command -v fd >/dev/null 2>&1; then
 		fd -e tex -e cls -e jpg -e png -e svg . . 2>/dev/null
 	else
 		ls ./**/*.{tex,cls,jpg,png,svg} 2>/dev/null
 	fi
-) | entr ${entr_args} -ns 'latexmk -R $0'
+) | entr ${entr_args} -ns "latexmk -R $vargs >&2" >/dev/null