#!/bin/sh
#
# fned - file:number editor
#
# reads file:number:blah (grep-style) from stdin or ARGV and opens the file in
# $EDITOR at that point (for vi/m). other editors just open file and print line
#
# usage: fned "$(git grep -n 'pattern' | fzf)"
#
# see also: plumb(1)

die() {
  echo "$*" 1>&2
  exit 1
}
[ -z "$1" ] && die "Usage: fned 'file:line:desc'"

fstr="$*"
file=${fstr%%:*}
# nofile=${fstr##$file:}
nofile=${fstr##"${file}":}
line=${nofile%%:*}

case "${EDITOR:-}" in
  vi*) ${EDITOR} -c "${line}" "${file}" ;;
  nvi*) ${EDITOR} -c "${line}" "${file}" ;;
  *)
    printf '%s\n' "fned: ${EDITOR} doesn't support line jumps. Go to ${line}"
    ${EDITOR} "${file}"
    ;;
esac
