Golf Me A Text-Editor Golf Me a Text Editor

(similar challenges: Create a simple line editor and Line editor (more text here))

I seem to have forgotten which program to use to open, read, and write text files. Something like Noteleaf or VSBro or ... You see my conundrum. But never fear! I know some people who can write one. But I'm a bit limited on space, so shorter programs would be much appreciated.

The Challenge

Create a program, in your language of choice, that can open, read, and write plaintext files. The program should take input from your language's equivalent of STDIN and write the contents of the file to STDOUT (or equivalent). The program should close the file after the user is done with it (when they type 'q'). If the file does not exist, then the program should create a new one with the given filename. Editing is done line-by-line. The user gives the line number they wish to edit, and then give the new content of that line. If there is no content given, the line should be removed. Additional specifications are listed below.

Commands

filename?&gt; - opens the specified file. If a file with the given name can not be found, create a new file. If the user types DELETE, then the program should prompt with delete?&gt;.
delete?&gt; - "deletes" the file. This is done by blanking it (i.e. replacing its contents with ""), which will suffice for this challenge. If the file can not be found, then print FILE NOT FOUND.
line?&gt; - the given line number is where the text will be changed when content?&gt; is given. If q is entered, then close the file and exit. If a is entered, then append a new empty line to the end of the file. If the index is out of bounds (i.e. does not exist (the text files can be 1 or 0 based indexed)) then print Error!.
content?&gt; - input that is given here will replace that of the previous content of the line given earlier (line?&gt;). If empty then delete that line (remove content from the line and shift all lines after it up one).  

After each filename?&gt; and content?&gt; the contents of the file should be printed, as well as -- followed by the number of lines in the file.

Examples

A (rather bad) example program in Python3 can be found here.  

Example deletions:

filename?&gt;DELETE
delete?&gt;passwords.txt


filename?&gt;DELETE
delete?&gt;evidence.txt
FILE NOT FOUND


Example run:

filename?&gt;passwords.txt
password
admin
1234
********
p@$$w0rd
c0d3_1$_l!f3
--
6
line?&gt;3
content?&gt;12345678
password
admin
12345678
********
p@$$w0rd
c0d3_1$_l!f3
--
6
line?&gt;4
content?&gt;
password
admin
12345678
p@$$w0rd
c0d3_1$_l!f3
--
6
line?&gt;a
password
admin
12345678
p@$$w0rd
c0d3_1$_l!f3


--
7
line?&gt;9
Error!

line?&gt;1
content?&gt;passcode
passcode
admin
12345678
p@$$w0rd
c0d3_1$_l!f3


--
7
line?&gt;5
content?&gt;
password
admin
12345678
p@$$w0rd


--
6
line?&gt;q


Remember, this is code-golf, so the shortest answer in bytes wins!
Bash, 388 375 characters

Note that I am aware that further clarifications may come which will invalidate my solution.
Feel free to carry out any necessary modification in the requirement.
I posted this because the challenge is complex and may help to see how someone else understood it when editing for clarification.

set -f
IFS=
read -pfilename?\&gt; f
[ $f = DELETE ]&amp;&amp;{
read -pdelete?\&gt; f
[ -f $f ]&amp;&amp;&gt;$f||echo FILE NOT FOUND
exit
}
[ -f $f ]&amp;&amp;mapfile t&lt;$f
echo "${t[*]}--"
while read -plineno?\&gt; l;do
[ $l = q ]&amp;&amp;break
[ $l = a ]&amp;&amp;t+=('
')||{ ((l&gt;=${#t[@]}))&amp;&amp;echo Error!&amp;&amp;continue
read -pcontent?\&gt; c
[ $c ]&amp;&amp;t[l]="$c
"||t=(${t[@]::l} ${t[@]:l+1})
}
echo "${t[*]}--"
done
echo -n "${t[*]}"&gt;$f


Thanks to:


GammaFunction for suggesting set -f to make some quoting unnecessary (13 characters).