From 91a717cb07758e04c7826243520a6f3b898125e7 Mon Sep 17 00:00:00 2001 From: Gustav Eek Date: Wed, 30 Nov 2022 10:32:35 +0100 Subject: [PATCH] [unoconv-display] Update for Emacs view mode Almost a full rewrite: (a) converted file name changed so that hash is reflected as a directory and the base name is kept for the PDF and text files, (b) convert to both PDF and text, and (c) complete with a flag (`-e`) for viewing the text version in Emacs. The name update has a positive pedagogic effect that the original filename is still reflected in the PDF/text viewer title. --- unoconv-display | 74 ++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/unoconv-display b/unoconv-display index 25fa465..8ba06f5 100644 --- a/unoconv-display +++ b/unoconv-display @@ -2,49 +2,67 @@ # -*- mode: shell-script -*- -# Convert to PDF and display a libreoffice compatible document read -# file on command line or else on stdandard in +# Convert to PDF and plain text and display a libreoffice compatible +# document read file on command line or else on stdandard in +# +# OPTIONS +# +# -e View file in Emacs client + +set -e DEBUG=1 -C="$HOME/.cache/unoconv-display" -tmp="/tmp/${0##*/}" -mkdir -p "$C" "$tmp" +cache="$HOME/.cache/unoconv-display" +tmp="$XDG_RUNTIME_DIR/${0##*/}" +mkdir -p "$cache" "$tmp" test -n "$DEBUG" &&\ echo "[$(date -Is)] ${0##*/} D: Start." >> $tmp/log -file=$1 -if [ -z "$file" ]; then - file="$tmp/$$" - cat > "$file" -fi -sha=$(sha1sum "$file"); -pdf="$C/${sha:0:16}.pdf" +if [ "$1" == "-e" ]; then fmt=emacs; shift; else fmt=pdf; fi +if [ -n "$1" ]; then file=$1 +else file="$tmp/$$"; cat > "$file"; fi +sha=$(sha1sum "$file") +base=$(basename ${file%.*}) +dir="$cache/${sha:0:16}" +pdf="$dir/$base.pdf" +txt="$dir/$base.txt" test -n "$DEBUG" &&\ - echo "[$(date -Is)] ${0##*/} D: Continue for '$tmp', '$file', '$pdf'." >> $tmp/log + echo "[$(date -Is)] ${0##*/} D: Continue for '$fmt', '$tmp', '$file', '$pdf', '$txt'." >> $tmp/log -if [ -f "$pdf" ]; then # Start PDF viewer - test -n "$DEBUG" &&\ - echo "[$(date -Is)] ${0##*/} D: Found '$pdf'. Start PDF viewer." >> $tmp/log - exec zathura --fork "$pdf" 2>> "/tmp/err" & +if [ -d "$dir" ]; then # Start text viewer rm -f "$tmp/$$" - exit 0 + test -n "$DEBUG" &&\ + echo "[$(date -Is)] ${0##*/} D: Found '$dir'. Start '$fmt' viewer." >> $tmp/log + case $fmt in + emacs) + exec emacsclient -e "(view-file \"$txt\")" > /dev/null + exit 0 + ;; + pdf) + exec zathura --fork "$pdf" 2>> "/tmp/err" & + exit 0 + ;; + *) + echo "[$(date -Is)] ${0##*/} E: Unknown format '$fmt'." 1>&2 + exit 1 + ;; + esac fi test -n "$DEBUG" &&\ - echo "[$(date -Is)] ${0##*/} D: Convert '$file' to '$pdf'." >> $tmp/log + echo "[$(date -Is)] ${0##*/} D: Convert '$file'." >> $tmp/log # Convert to PDF +mkdir "$dir" unoconv -f pdf -o "$pdf" "$file" || \ unoconv -l && \ unoconv -f pdf -o "$pdf" "$file" +unoconv -f txt -o "$txt" "$file" -if [ -f "$pdf" ]; then # Recursively start evince - test -n "$DEBUG" &&\ - echo "[$(date -Is)] [$(date -Is)] ${0##*/} D: Recursively for '$file'." >> $tmp/log - bash ${0} "$file" - exit $? -fi - -echo "[$(date -Is)] [$(date -Is)] ${0##*/} E: Error. Why are we here?" >> $tmp/log -exit 1 +# Recursively open +test -n "$DEBUG" &&\ + echo "[$(date -Is)] [$(date -Is)] ${0##*/} D: Recursively for '$file'." >> $tmp/log +if [ $fmt == "emacs" ]; then args=-e; fi +bash ${0} ${args} "$file" +exit $? -- 2.39.2