source "$HOME/.ad/lib/ad.sh"
FTYPE_COMMENT_STRS="$HOME/.ad/lib/ftype-comment-chars.txt"
requireAd
addr="$(bufRead "$AD_BUFID" addr | sed -E 's/(.+):.+,(.+):.+/\1,\2/')"
ftype="$(bufRead "$AD_BUFID" filetype)"
comment_str="$(grep "$ftype" "$FTYPE_COMMENT_STRS" | cut -d' ' -f2)"
if [ -z "$comment_str" ]; then
adCtl "echo no configured comment string for $ftype"
exit 1
fi
echo '-,/\n/' | bufWrite "$AD_BUFID" xaddr
xdot="$(bufRead "$AD_BUFID" xdot)"
n_lines="$(echo "$xdot" | wc -l)"
n_comment_lines="$(echo "$xdot" | grep "^\\s*$comment_str" | wc -l)"
if [ "$n_lines" != "$n_comment_lines" ]; then
adEdit "-,/\\n/ x/.*\\n/ i:$comment_str :"
else
adEdit "-,/\\n/ x/.+\\n/ n:^(\\s*?)$comment_str ?: c/{1}/"
fi
echo "$addr" | bufWrite "$AD_BUFID" addr
adCtl "echo addr was '$addr'"