patch 9.0.1439: start Insert mode when accessing a hidden prompt buffer

Problem:    Start Insert mode when accessing a hidden prompt buffer.
Solution:   Call leaving_window() in aucmd_restbuf(). (Thorben Tröbst,
            closes #12148, closes #12147)
diff --git a/src/testdir/test_prompt_buffer.vim b/src/testdir/test_prompt_buffer.vim
index 9998cc5..28e0d76 100644
--- a/src/testdir/test_prompt_buffer.vim
+++ b/src/testdir/test_prompt_buffer.vim
@@ -252,4 +252,42 @@
   call StopVimInTerminal(buf)
 endfunc
 
+func Test_prompt_appending_while_hidden()
+  call CanTestPromptBuffer()
+
+  let script =<< trim END
+      new prompt
+      set buftype=prompt
+      set bufhidden=hide
+
+      func s:TextEntered(text)
+          if a:text == 'exit'
+              close
+          endif
+          echowin 'Entered:' a:text
+      endfunc
+      call prompt_setcallback(bufnr(), function('s:TextEntered'))
+
+      func DoAppend()
+        call appendbufline('prompt', '$', 'Test')
+      endfunc
+  END
+  call writefile(script, 'XpromptBuffer', 'D')
+
+  let buf = RunVimInTerminal('-S XpromptBuffer', {'rows': 10})
+  call TermWait(buf)
+
+  call term_sendkeys(buf, "asomething\<CR>")
+  call TermWait(buf)
+
+  call term_sendkeys(buf, "exit\<CR>")
+  call TermWait(buf)
+
+  call term_sendkeys(buf, ":call DoAppend()\<CR>")
+  call TermWait(buf)
+  call assert_notmatch('-- INSERT --', term_getline(buf, 10))
+
+  call StopVimInTerminal(buf)
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab