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.
This commit is contained in:
9
.config/vim/ftplugin/zettel.vim
Normal file
9
.config/vim/ftplugin/zettel.vim
Normal file
@@ -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
|
||||||
@@ -40,6 +40,8 @@ if [[ $OSTYPE =~ darwin && ! $PATH =~ "/Library/Apple/usr/bin" ]]; then
|
|||||||
export PATH="${PATH:+$PATH:}/Library/Apple/usr/bin"
|
export PATH="${PATH:+$PATH:}/Library/Apple/usr/bin"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
export ZETTELKASTEN_NOTES="$HOME/docs"
|
||||||
|
|
||||||
# Locale settings as $LANG
|
# Locale settings as $LANG
|
||||||
[[ ! -e "$XDG_CONFIG_HOME/locale.conf" ]] || . "$XDG_CONFIG_HOME/locale.conf"
|
[[ ! -e "$XDG_CONFIG_HOME/locale.conf" ]] || . "$XDG_CONFIG_HOME/locale.conf"
|
||||||
|
|
||||||
|
|||||||
57
.config/zsh/autoload/zk
Executable file
57
.config/zsh/autoload/zk
Executable file
@@ -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
|
||||||
|
#
|
||||||
|
|
||||||
|
<!--- vim: set ft=markdown.zettel: -->
|
||||||
|
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
|
||||||
Reference in New Issue
Block a user