From afd5d3b2fe35051e5cc4787de288dcad79c89426 Mon Sep 17 00:00:00 2001 From: bth Date: Mon, 13 Jan 2025 21:20:00 -0800 Subject: [PATCH] um last opens the file with emacsclient, except when run by um next or testing --- shell/last.sh | 23 +++++++++++++++-------- shell/next.sh | 9 +++++---- shell/test/um.bats | 7 +++---- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/shell/last.sh b/shell/last.sh index a58ea32..93836a3 100755 --- a/shell/last.sh +++ b/shell/last.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Get the last file name of any numbered series of text files. +# Get the last file name of any numbered series of text files and open it with emacs. # file format: # in regex terms: @@ -9,13 +9,20 @@ # [leading numbers with consistent width].[optional string descriptor and . separator][md|txt] UMALLFILES=`ls | grep -E '^[[:digit:]].*\.(md|txt)'` -LASTSTATUS=$? +UMLASTSTATUS=$? +UMLASTFILE='' -# NOTE: if we don't stop and signal here, tail will exit 0 -if [ "$LASTSTATUS" -ne 0 ]; then - echo "no files found" >&2 - exit 1 +if [ "$UMLASTSTATUS" -ne 0 ]; then + UMLASTFILE="$UMDEFAULTINIT" +else + # NOTE: if var isn't quoted echo will change newlines to spaces + UMLASTFILE=`echo "$UMALLFILES" | tail -n 1` fi -# NOTE: if var isn't quoted echo will change newlines to spaces -echo "$UMALLFILES" | tail -n 1 +if [ "$UMDISABLEEMACS" = true ] || [ "$UMTEST" = true ] ; then + echo $UMLASTFILE + exit 0 +fi + +echo "emacsclient -n $UMLASTFILE" +emacsclient -n $UMLASTFILE diff --git a/shell/next.sh b/shell/next.sh index e54db7c..2b5f3a5 100755 --- a/shell/next.sh +++ b/shell/next.sh @@ -23,8 +23,8 @@ # # See https://git.bth.moi/bth/um#next -# NOTE: handle the exit 1 case of last.sh -LASTFILE=`$UMBASEPATH/last.sh || echo $UMDEFAULTINIT` +# NOTE: we only want the filename, not to run emacs yet. +LASTFILE=`UMDISABLEEMACS=true; $UMBASEPATH/last.sh` # NOTE: the optional $1 arg passed to awk with -v: NEXTFILE=`echo $LASTFILE | awk -f $UMBASEPATH/next.awk -v arg=$1` @@ -37,11 +37,12 @@ else fi # for testing and piping -if [ $UMNEXTPRINT ]; then +# NOTE: bash booleans are such a pain, because they don't exist. https://stackoverflow.com/a/2953673 +if [ "$UMNEXTPRINT" = true ]; then # NOTE: printf instead of echo, because echo inserts a newline, and we pipe # this back into elisp for um-next-shell: printf $NEXTFILE -elif [ $UMNEXTTEST ]; then +elif [ "$UMTEST" = true ]; then echo $UMELISP else # NOTE: emacs won't accept piped input as a file name, so we use --eval to run diff --git a/shell/test/um.bats b/shell/test/um.bats index 20646de..3c6542c 100644 --- a/shell/test/um.bats +++ b/shell/test/um.bats @@ -13,11 +13,11 @@ setup() { export ORIGPWD=`pwd` cd $DIR/mockjournal # so that we can run next.sh without dealing with emacsclient. - export UMNEXTTEST=true + export UMTEST=true } teardown() { - export UMNEXTTEST=false + export UMTEST=false cd $ORIGPWD git restore $DIR/mockjournal } @@ -73,8 +73,7 @@ um_next_awk() { @test "um last empty" { cd foo run um last - assert_failure - assert_output 'no files found' + assert_output '000.md' } @test "um cat" {