commit ad22b936626f5e473c4625564b8d0c616e594424
parent 56df91d05d8edf72abdf6d85e4b436c59e925716
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Tue, 30 Jun 2020 11:43:01 +0200
Rename document compiler and improve help text
Diffstat:
M | .exrc | | | 6 | +++--- |
A | .local/bin/cdoc | | | 143 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
D | .local/bin/compile | | | 140 | ------------------------------------------------------------------------------- |
3 files changed, 146 insertions(+), 143 deletions(-)
diff --git a/.exrc b/.exrc
@@ -26,8 +26,8 @@ map =N :set nonumber
" add pipe symbol by prefix with ^V character (press C-v twice): |
map qB :Edit $BIB
-map qc :!compile %
-map qC :!tmux new-window -c "$(pwd)" compile -c -o % \; previous-window
+map qc :!cdoc %
+map qC :!tmux new-window -c "$(pwd)" cdoc -c -o % \; previous-window
map qd :r !date
map qD :r !date +'\%m/\%d'
A
map qe :edit
@@ -35,7 +35,7 @@ map qj :r !jabbrev
map qm :!make
map qM :!tmux split-window -p 25 make \; select-pane -l
map qn I- [ ]
-map qo :!compile -o %
+map qo :!cdoc -o %
map qp :r !xclip -o
map qP :r !xclip -o -selection clipboard
map qr :r !scholarref
diff --git a/.local/bin/cdoc b/.local/bin/cdoc
@@ -0,0 +1,143 @@
+#!/bin/sh
+opener=xdg-open
+
+show_help() {
+ printf 'usage: %s [OPTIONS] FILE ...\n' "${0##*/}"
+ printf 'compiles each FILE to pdf. Supported input file types are:\n'
+ printf ' - latex (extension .tex) including dependency recursion\n'
+ printf ' - gnuplot (extension .gp)\n'
+ printf ' - mandoc (extension .[0-9])\n'
+ printf ' - groff/mom (extension .mom)\n'
+ printf 'OPTIONS are one or more of the following:\n'
+ printf ' -h show this message\n'
+ printf ' -c continuously recompile upon file changes (requires entr(1))\n'
+ printf ' -o open output file after compiling (requires %s(1))\n' "$opener"
+ printf ' -- do not consider any following args as options\n'
+}
+
+die() {
+ printf '%s: error: %s\n' "${0##*/}" "$1" >&2
+ exit 1
+}
+
+regeximatch() {
+ printf '%s' "$1" | grep -iEq "$2"
+}
+
+extract_tex_include() {
+ d="$(grep -v ".*%.*\\${2}" "$1" |
+ sed -nE "/.*\\${2}\\[*.*\\]*\\{([^}]*)\\}.*/{ s//\\1/;p; }")"
+ if [ "$2" = "bibliography" ] && [ -n "$d" ]; then
+ d="${d%.bib}.bib"
+ fi
+ if [ -n "$d" ]; then
+ printf '%s\n' "$d"
+ fi
+}
+
+find_dependencies() {
+ case "$1" in
+ *.tex)
+ printf '%s\n' "$1"
+ if grep -q -e '\input{' \
+ -e '\include[graphics]' \
+ -e '\addbibresource{' \
+ -e '\bibliography{' \
+ "$1"; then
+ extract_tex_include "$1" 'input'
+ extract_tex_include "$1" 'include'
+ extract_tex_include "$1" 'includegraphics'
+ extract_tex_include "$1" 'addbibresource'
+ extract_tex_include "$1" 'bibliography'
+ fi;;
+ *)
+ die "cannot find dependencies in unknown file type $1";;
+ esac
+}
+
+run_compile() {
+ if [ "$continuous" = 1 ]; then
+ printf '%s\n' "$2" | entr -s "$1"
+ else
+ eval "$1"
+ fi
+}
+
+handle_target() {
+ base="$(basename "${1%.*}")"
+ if [ -e "$1" ]; then
+ case "$1" in
+ *.tex)
+ deps="$1"
+ deps="$(find_dependencies "$1")"
+ for d in $deps; do
+ if [ "${d##*.}" = "tex" ]; then
+ deps="$(printf '%s\n%s\n' "${deps}" "$(find_dependencies "$d")")"
+ fi
+ done
+ deps="$(printf '%s\n' "$deps" | sort | uniq)"
+ printf 'deps=%s\n' "$deps"
+ if regeximatch "$deps" '\.bib'; then
+ compile_string="pdflatex '$1'; bibtex '${1%.tex}'; pdflatex '$1'; pdflatex '$1'"
+ else
+ compile_string="pdflatex '$1' && pdflatex '$1'"
+ fi
+ run_compile "$compile_string" "$deps"
+ if [ "$open" = 1 ]; then
+ $opener "${1%.tex}.pdf"
+ fi
+ ;;
+ *.gp)
+ run_compile "gnuplot '$1'" "$1";;
+ *.[0-9])
+ out="$(basename "${base}").pdf"
+ if [ "$open" = 1 ]; then
+ run_compile "refer -PS -e '$1' | groff -mandoc -T pdf > '$out' && $opener '$out'" "$1"
+ else
+ run_compile "refer -PS -e '$1' | groff -mandoc -T pdf > '$out'" "$1"
+ fi;;
+ *.mom)
+ out="$(basename "${base}").pdf"
+ if [ "$open" = 1 ]; then
+ run_compile "pdfmom '$1' > '$out' && $opener '$out'" "$1"
+ else
+ run_compile "pdfmom '$1' > '$out'" "$1"
+ fi;;
+ *)
+ die "File type ${1##*.} not supported\\n";;
+ esac
+ else
+ die "cannot read $1"
+ fi
+}
+
+continuous=0
+open=0
+while :; do
+ case "$1" in
+ -h|-\?)
+ show_help
+ exit 0;;
+ -c)
+ continuous=1;;
+ -o)
+ open=1;;
+ --) # end all options
+ shift
+ break;;
+ -?*)
+ die 'unknown option specified';;
+ *) # No more options
+ break
+ esac
+ shift
+done
+
+if [ $# -lt 1 ]; then
+ show_help
+ exit 1
+else
+ for f in "$@"; do
+ handle_target "$f"
+ done
+fi
diff --git a/.local/bin/compile b/.local/bin/compile
@@ -1,140 +0,0 @@
-#!/bin/sh
-opener=xdg-open
-
-show_help() {
- echo "usage: ${0##*/} [OPTIONS] FILE ..."
- echo "compiles each FILE based on its extension."
- echo
- echo "OPTIONS are one or more of the following:"
- echo " -h show this message"
- echo " -c continuously recompile upon file changes (requires entr(1))"
- echo " -o open output file after compiling (requires ${opener}(1))"
- echo " -- do not consider any following args as options"
-}
-
-die() {
- printf '%s: error: %s\n' "${0##*/}" "$1" >&2
- exit 1
-}
-
-regeximatch() {
- printf '%s' "$1" | grep -iEq "$2"
-}
-
-extract_tex_include() {
- d="$(grep -v ".*%.*\\${2}" "$1" |
- sed -nE "/.*\\${2}\\[*.*\\]*\\{([^}]*)\\}.*/{ s//\\1/;p; }")"
- if [ "$2" = "bibliography" ] && [ -n "$d" ]; then
- d="${d%.bib}.bib"
- fi
- if [ -n "$d" ]; then
- printf '%s\n' "$d"
- fi
-}
-
-find_dependencies() {
- case "$1" in
- *.tex)
- printf '%s\n' "$1"
- if grep -q -e '\input{' \
- -e '\include[graphics]' \
- -e '\addbibresource{' \
- -e '\bibliography{' \
- "$1"; then
- extract_tex_include "$1" 'input'
- extract_tex_include "$1" 'include'
- extract_tex_include "$1" 'includegraphics'
- extract_tex_include "$1" 'addbibresource'
- extract_tex_include "$1" 'bibliography'
- fi;;
- *)
- die "cannot find dependencies in unknown file type $1";;
- esac
-}
-
-run_compile() {
- if [ "$continuous" = 1 ]; then
- printf '%s\n' "$2" | entr -s "$1"
- else
- eval "$1"
- fi
-}
-
-handle_target() {
- base="$(basename "${1%.*}")"
- if [ -e "$1" ]; then
- case "$1" in
- *.tex)
- deps="$1"
- deps="$(find_dependencies "$1")"
- for d in $deps; do
- if [ "${d##*.}" = "tex" ]; then
- deps="$(printf '%s\n%s\n' "${deps}" "$(find_dependencies "$d")")"
- fi
- done
- deps="$(printf '%s\n' "$deps" | sort | uniq)"
- printf 'deps=%s\n' "$deps"
- if regeximatch "$deps" '\.bib'; then
- compile_string="pdflatex '$1'; bibtex '${1%.tex}'; pdflatex '$1'; pdflatex '$1'"
- else
- compile_string="pdflatex '$1' && pdflatex '$1'"
- fi
- run_compile "$compile_string" "$deps"
- if [ "$open" = 1 ]; then
- $opener "${1%.tex}.pdf"
- fi
- ;;
- *.gp)
- run_compile "gnuplot '$1'" "$1";;
- *.[0-9])
- out="$(basename "${base}").pdf"
- if [ "$open" = 1 ]; then
- run_compile "refer -PS -e '$1' | groff -mandoc -T pdf > '$out' && $opener '$out'" "$1"
- else
- run_compile "refer -PS -e '$1' | groff -mandoc -T pdf > '$out'" "$1"
- fi;;
- *.mom)
- out="$(basename "${base}").pdf"
- if [ "$open" = 1 ]; then
- run_compile "pdfmom '$1' > '$out' && $opener '$out'" "$1"
- else
- run_compile "pdfmom '$1' > '$out'" "$1"
- fi;;
- *)
- die "File type ${1##*.} not supported\\n";;
- esac
- else
- die "cannot read $1"
- fi
-}
-
-continuous=0
-open=0
-while :; do
- case "$1" in
- -h|-\?)
- show_help
- exit 0;;
- -c)
- continuous=1;;
- -o)
- open=1;;
- --) # end all options
- shift
- break;;
- -?*)
- die 'unknown option specified';;
- *) # No more options
- break
- esac
- shift
-done
-
-if [ $# -lt 1 ]; then
- show_help
- exit 1
-else
- for f in "$@"; do
- handle_target "$f"
- done
-fi