From 84cb259930d7fe3cdbb91dedc43c30c274f10232 Mon Sep 17 00:00:00 2001
From: crater2150 <crater@qwertyuiop.de>
Date: Thu, 2 Apr 2020 18:22:10 +0200
Subject: [PATCH] Add tmpsrc plugin

---
 plugins/tmpsrc/tmpsrc.plugin.zsh | 52 ++++++++++++++++++++++++++++++++
 zplug.zsh                        |  1 +
 2 files changed, 53 insertions(+)
 create mode 100755 plugins/tmpsrc/tmpsrc.plugin.zsh

diff --git a/plugins/tmpsrc/tmpsrc.plugin.zsh b/plugins/tmpsrc/tmpsrc.plugin.zsh
new file mode 100755
index 0000000..83af31a
--- /dev/null
+++ b/plugins/tmpsrc/tmpsrc.plugin.zsh
@@ -0,0 +1,52 @@
+#!/bin/zsh
+
+tmpsrc() {
+    if [[ -z $1 ]]; then
+	echo "Usage: tmpsrc <LANGUAGE>"
+	return 1
+    fi
+    local confdir="${XDG_CONFIG_HOME:-$HOME/.config}/tmpsrc"
+    local template=$1
+    local templatepath=$confdir/$template
+    shift
+
+    local tempname=$(mktemp -u -t tmpsrc-XXXXXXXX)
+
+    if [[ -d $templatepath ]]; then
+	cp -r $templatepath $tempname
+	if [[ ! -v TMUX ]]; then
+	    tmux new-session -d -s $tempname -c $tempname -- $EDITOR $(realpath $tempname/tmpsrc-mainfile)
+	    if [[ -x $templatepath/tmpsrc-compiler ]]; then
+		tmux split -h -c $tempname -t $tempname $templatepath/tmpsrc-compiler "$@"
+		tmux select-pane -L -t $tempname
+	    fi
+	    tmux attach -t $tempname
+	else
+	    pushd $tempname
+	    if [[ -x $templatepath/tmpsrc-compiler ]]; then
+		tmux split -h -c $tempname $templatepath/tmpsrc-compiler "$@"
+		tmux select-pane -L
+	    fi
+	    $EDITOR $(realpath $tempname/tmpsrc-mainfile)
+	    popd
+	fi
+    elif [[ -x $templatepath ]]; then
+	mkdir $tempname
+	pushd $tempname
+	if [[ ! -v TMUX ]]; then
+	    tmux new-session -s $tempname -- $templatepath "$@"
+	else
+	    $templatepath "$@"
+	fi
+	popd
+    elif [[ -e $templatepath ]]; then
+	cp $templatepath $tempname
+	$EDITOR $tempname
+    else
+	echo "No template for $template"
+	return 1
+    fi
+
+    rm -rf $tempname
+}
+
diff --git a/zplug.zsh b/zplug.zsh
index 433c66c..bf04742 100644
--- a/zplug.zsh
+++ b/zplug.zsh
@@ -9,6 +9,7 @@ zplug 'crater2150-zsh/chroma-z', as:theme
 
 zplug $ZDOTDIR/plugins/highlight-config, from:local, defer:3
 zplug $ZDOTDIR/plugins/bundled-compdefs, from:local
+zplug $ZDOTDIR/plugins/tmpsrc, from:local
 
 zplug 'molovo/revolver', \
   as:command, \