From 91b82725558280fb3ca2bac9c45455e74d3c6757 Mon Sep 17 00:00:00 2001 From: bth Date: Sun, 14 Jan 2024 09:46:11 -0800 Subject: [PATCH] split out to next.awk --- bash/next.awk | 35 +++++++++++++++++ bash/next.sh | 103 +++----------------------------------------------- 2 files changed, 41 insertions(+), 97 deletions(-) create mode 100644 bash/next.awk diff --git a/bash/next.awk b/bash/next.awk new file mode 100644 index 0000000..c5e19c3 --- /dev/null +++ b/bash/next.awk @@ -0,0 +1,35 @@ +# awk script to increment the digits, preserve the zero prefix, add string descriptor if supplied, and add back extension + +BEGIN { +# set field separator, blank by default +FS = "." +} + +{ +newnum = $1 + 1 +nzeros = length($1) - length(int(newnum)) + +# could I do this with a simpler regex to select all leading zeros? +leadingzeros = "" +if (nzeros > 0) + for (i = 1; i <= nzeros; i++) + leadingzeros = sprintf("%s%s", leadingzeros, "0") + +if (nzeros > 0) + newnum = sprintf("%s%s", leadingzeros, newnum) + +# start by prefixing the number, now with leading zeros +finalstr = newnum + +# allow an arg input to fill out descriptor string. +if (arg != "") + finalstr = sprintf("%s.%s", finalstr, arg) + +# append last field by default +finalstr = sprintf("%s.%s", finalstr, $NF) + +} + +END { +print finalstr +} diff --git a/bash/next.sh b/bash/next.sh index 6dbbd10..86d833b 100755 --- a/bash/next.sh +++ b/bash/next.sh @@ -1,12 +1,12 @@ # Get the file name in any numbered series of text files and open with emacs # Usage: -# $ . next [descriptor] +# $ next [descriptor] # Handles an optional string descriptor. Like this: # $ ls # 011.blah.md -# $ . next foo +# $ next foo # 012.foo.md # File format: @@ -15,100 +15,9 @@ # in plain terms: # [leadings numbers with consistent width].[optional string descriptor and . separator][md|txt] -# what this does: -# ls | grep [regex for number.*.md] | get last | awk script to increment the digits, preserve the zero prefix, add string descriptor if supplied, and add back extension - -# Note the egrep '^\d.*\.md|txt' (matches whole string) to account for an optional string between the initial number and the file extension. As in '435.blah.md'. - -# old version, relied on python, which didn't handle leading zeros properly, but treated them as binary: -# ls | grep '\d.*\.md' | tail -n 1 | sed s/'\..*md'// | sed s/.*/'print & + 1'/ | python - | sed s/.*/'&.md/' - -# BUGS - -# 1. Emacs won't accept piped input as a file name. I'd rather keep this as a separate utility, which only printed the right filename, and then use an alias to pipe the filename to emacs, or invoke it manually. - -# A. -# What I want, is this syntax: -# next [optional arg] -# To both get the file and open emacs with it. - -# B. -# But this doesn't work: -# next | emacs -# Because it sees the stdin the pipe wants to feed it as a substitute for the tty in the new session, rather than as input for startup only, i.e. the filename. - -# C. -# Wrapping the whole shell command here works, like this: -# emacs `ls | grep ...` - -# But emacs doesn't like being run inside a child level shell, because it can't load .emacs properly. So you must run this file within the current shell using ".", like this: -# . next -# . next foo - -# D. -# This does not work, because .profile must be executed, meaning that the "" expression is peeked by the shell: -# alias next="emacs `next.sh $1`" - -# E. -# This does not work either, because the arg never gets filled out when evoked: -# alias next='emacs `next.sh $1`' - -# F. -# This works and is the current solution: -# alias next='. next.sh' - -# Which relies on 2 somewhat obscured facts: -# 1. This script calls emacs itself, wrapping the real work in `` -# 2. The alias is a simple substitution, which fortunately seems to happen before args are processed by the shell, meaning that the shell sees: ". next.sh arg" -# 3. NOTE: the symbolic link in the PATH should not be simply "next", because it -# may in certain situations conflict with the alias. The link should be "next.sh" -> ~/config/.. -# 4. NOTE: I use 'e' rather than 'emacs', to use my clever emacs/emacsclient function. - - -NEXTFILE=`$UMBASEPATH/last.sh | awk ' - -BEGIN { -# set field separator, blank by default -FS = "." -} - -{ -newnum = $1 + 1 -nzeros = length($1) - length(int(newnum)) - -# could I do this with a simpler regex to select all leading zeros? -leadingzeros = "" -if (nzeros > 0) - for (i = 1; i <= nzeros; i++) - leadingzeros = sprintf("%s%s", leadingzeros, "0") - -if (nzeros > 0) - newnum = sprintf("%s%s", leadingzeros, newnum) - -# start by prefixing the number, now with leading zeros -finalstr = newnum - -# allow an arg input to fill out descriptor string. -# single quote break to allow the shell to fill it out, double quotes so awk will treat it as a string -arg = "'$1'" - -if (arg != "") - finalstr = sprintf("%s.%s", finalstr, arg) - -# append last field by default -finalstr = sprintf("%s.%s", finalstr, $NF) - -# previous loop to reassemble all fields. But I do not want the old middle string here -# Keeping it here for reference. -# for (i = 2; i <= NF; i++) -# finalstr = sprintf("%s.%s", finalstr, $i) -# } - -} - -END { -print finalstr -}' -` +# NOTE the optional $1 arg passed to awk with -v: +NEXTFILE=`$UMBASEPATH/last.sh | awk -f $UMBASEPATH/next.awk -v arg=$1` +# HACK: emacs won't accept piped input as a file name, so we use --eval to open +# the file via find-file. emacsclient --eval "(progn (find-file \"$NEXTFILE\") (uldb-journal-header) (message \"creating $NEXTFILE\"))"