From 5df9e4d6eeb2fb9b98df90159d36f353203dc2ae Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Mon, 28 Mar 2022 23:48:51 +0200 Subject: [PATCH] zsh,vim: Add WIP Zettelkasten tooling Add `zk`, an autoloadable function that creates a new zettel after a template in the right directory. Add `zettel` vim ftplugin. --- .config/vim/ftplugin/zettel.vim | 9 ++++++ .config/zsh/.zprofile | 2 ++ .config/zsh/autoload/zk | 57 +++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 .config/vim/ftplugin/zettel.vim create mode 100755 .config/zsh/autoload/zk diff --git a/.config/vim/ftplugin/zettel.vim b/.config/vim/ftplugin/zettel.vim new file mode 100644 index 0000000..0bbb353 --- /dev/null +++ b/.config/vim/ftplugin/zettel.vim @@ -0,0 +1,9 @@ +" Reuse git-commit textwidths for subject (minus the `# ` markdown +" header-prefix) and body. This way the note (but especially the subject) should +" be usable as a commit message. +setlocal colorcolumn+=53 +setlocal textwidth=72 +" Spell checking always enabled +setlocal spell spelllang=en +" Automatic formatting +setlocal formatoptions+=a diff --git a/.config/zsh/.zprofile b/.config/zsh/.zprofile index 61c475c..bac1643 100644 --- a/.config/zsh/.zprofile +++ b/.config/zsh/.zprofile @@ -40,6 +40,8 @@ if [[ $OSTYPE =~ darwin && ! $PATH =~ "/Library/Apple/usr/bin" ]]; then export PATH="${PATH:+$PATH:}/Library/Apple/usr/bin" fi +export ZETTELKASTEN_NOTES="$HOME/docs" + # Locale settings as $LANG [[ ! -e "$XDG_CONFIG_HOME/locale.conf" ]] || . "$XDG_CONFIG_HOME/locale.conf" diff --git a/.config/zsh/autoload/zk b/.config/zsh/autoload/zk new file mode 100755 index 0000000..fc045a9 --- /dev/null +++ b/.config/zsh/autoload/zk @@ -0,0 +1,57 @@ +#!/usr/bin/env zsh + +# Create a new zettel in the zettelkasten +# Expects $ZETTELKASTEN_NOTES to be set to the root of the zettelkasten + +emulate -L zsh -o errreturn + +err() { + [[ -z $1 ]] || >&2 printf "%s\n" "$1" +} + +die() { + err "$1" + return ${2:-1} +} + +[[ $ZETTELKASTEN_NOTES ]] || die '$ZETTELKASTEN_NOTES not set' +pushd -q "$ZETTELKASTEN_NOTES" + +setopt NO_ERR_RETURN + +# Use year/month/day/time as directory and create it, if does not exist +dir="$(date +"%Y/%m/%d/%H%M%S")" +mkdir -p "$dir" + +if [[ ! -e "$dir/README.md" ]]; then + # Paste zettel template + cat >"$dir/README.md" <<-EOF + # + + + EOF +else + # For the (not indented) case that the README.md already exists, remember + # that so that it does not get deleted later on. + existed=1 +fi + +"${EDITOR:-vim}" "$dir/README.md" +errc=$? + +if (( $errc )); then + # Delete remainders on error + [[ $existed ]] || rm -f "$dir/README.md" || true + rmdir -p "$dir" 2>/dev/null || true +else + # Append subject to directory name + escaped_name="$( + head -n1 "$dir/README.md" \ + | sed -E 's/^(#* )?//; s/[^-a-zA-Z0-9_.]/-/g' \ + | tr '[:upper:]' '[:lower:]' + )" + mv "$dir" "$dir-$escaped_name" +fi + +popd -q +return $errc