#!/bin/sh

#
# Copyright (c) 2000 Lars Bernhardsson, see README for licence details
#
# sample.editor - This starts an editor on a selected file
#
# It finds out which file and in what dir it exists by reading
# the larswm environment variables that are set when an app
# is launched while some text is highlighted.
#
# If no file is selected, it starts the editor in the current working
# directory of the focused terminal window.
#
# For this to work, your window titles need to be set up as follows:
#
#   user@host: /current/working/directory
#
# This format is default in newer RedHat Linux versions.
#
# Here is an example of how to set this up when using bash.
#
# Put these lines in your .bashrc:
#
# if [ "$PS1" ]; then
#   case $TERM in
#     xterm*)
#       PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
#       ;;
#     esac
# fi
#

# What editor do you want to launch?
EDITOR=gvim

# Figure out what dir we are in
DIR=`echo "$WM_FOCUSNAME" | cut -d\  -f2`

# If the dir cannot be determined, just launch editor
[ ! -d $DIR ] && exec $EDITOR

# Else go to the directory before proceeding
cd $DIR

# If the highlighted text does not name an existing file, just launch editor
[ ! -f "$WM_SELTEXT" ] && exec $EDITOR

# Launch the editor on the highlighted filename
exec $EDITOR "$WM_SELTEXT"
