patch 8.0.1732: crash when terminal API call deletes the buffer

Problem:    Crash when terminal API call deletes the buffer.
Solution:   Lock the buffer while calling a function. (closes #2813)
diff --git a/src/buffer.c b/src/buffer.c
index 4563736..c3d3d77 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -417,6 +417,8 @@
 	hash_remove(&buf_hashtab, hi);
 }
 
+static char *e_buflocked = N_("E937: Attempt to delete a buffer that is in use");
+
 /*
  * Close the link to a buffer.
  * "action" is used when there is no longer a window for the buffer.
@@ -476,8 +478,15 @@
 	if (term_job_running(buf->b_term))
 	{
 	    if (wipe_buf || unload_buf)
+	    {
+		if (buf->b_locked)
+		{
+		    EMSG(_(e_buflocked));
+		    return;
+		}
 		/* Wiping out or unloading a terminal buffer kills the job. */
 		free_terminal(buf);
+	    }
 	    else
 	    {
 		/* The job keeps running, hide the buffer. */
@@ -499,7 +508,7 @@
      * halfway a command that relies on it). Unloading is allowed. */
     if (buf->b_locked > 0 && (del_buf || wipe_buf))
     {
-	EMSG(_("E937: Attempt to delete a buffer that is in use"));
+	EMSG(_(e_buflocked));
 	return;
     }
 
@@ -1356,6 +1365,12 @@
 	int	forward;
 	bufref_T bufref;
 
+	if (buf->b_locked)
+	{
+	    EMSG(_(e_buflocked));
+	    return FAIL;
+	}
+
 	set_bufref(&bufref, buf);
 
 	/* When unloading or deleting a buffer that's already unloaded and