-# Print plain message and exit
-function message {
- while [ "$1" == '-h' ]; do
- shift; echo "$1"; shift
- done
- header text/plain
- echo "$*"
- exit
+# Format and output a file
+function format {
+ # Create a temp file with the provided modeline
+ tmp="$(mktemp)"
+ sed -e "1ivim: $(get_modeline)" \
+ -e "\$avim: $(get_modeline)" "$1" > "$tmp"
+
+ # Determine cache name
+ md5="$(cat index.cgi vimrc "$tmp" /usr/bin/ex | md5sum -b)"
+ out="cache/${md5% *}.htm"
+ zip="$out.gz"
+
+ # Cache the file, if needed
+ if [ ! -f "$zip" ]; then
+ # - I have some plugins in ~/.vim
+ # - Run ex in pty to trick it into thinking that it
+ # has a real terminal, note that we also have to set
+ # term=xterm-256color in vimrc
+ HOME=/home/andy \
+ /home/vpaste/bin/pty \
+ /usr/bin/ex -nXZ -i NONE -u vimrc \
+ '+sil! set fde= fdt= fex= inde= inex= key= pa= pexpr=' \
+ '+sil! set iconstring= ruf= stl= tal=' \
+ "+sil! set titlestring=$1\ -\ vpaste.net" \
+ '+sil! set noml' \
+ '+sil! 1d|$d|'$2 \
+ '+sil! %s/\r//g' \
+ '+sil! TOhtml' \
+ "+sav! $out" \
+ '+qall!' \
+ "$tmp" >/dev/null 2>&1
+ gzip "$out"
+ fi
+ rm "$tmp"
+
+ # Output the file
+ respond -y -c "text/html" -f "$zip"