@@ -23,6 +23,7 @@ type input_buffer = {
23
23
mutable str : Bytes .t ; (* buffer of already read characters *)
24
24
mutable len : int ; (* number of chars in the buffer *)
25
25
mutable bols : int list ; (* offsets in str of beginning of lines *)
26
+ mutable stream : (unit ,char ) Gramlib.Stream .t ; (* stream of chars *)
26
27
mutable tokens : Pcoq.Parsable .t ; (* stream of tokens *)
27
28
mutable start : int } (* stream count of the first char of the buffer *)
28
29
@@ -33,10 +34,23 @@ let resize_buffer ibuf = let open Bytes in
33
34
blit ibuf.str 0 nstr 0 (length ibuf.str);
34
35
ibuf.str < - nstr
35
36
37
+ let peek_to_newline ibuf =
38
+ (* peek to see a newline following the latest command
39
+ cf #19355 *)
40
+ let rec aux n =
41
+ let l = Gramlib.Stream. npeek () n ibuf.stream in
42
+ if List. length l < n then ()
43
+ else match CList. last l with
44
+ | '\n' -> ()
45
+ | ' ' | '\t' -> aux (n+ 1 )
46
+ | _ -> (* nonblank character: the latest command is not the last on this line *) ()
47
+ in
48
+ aux 1
49
+
36
50
(* Delete all irrelevant lines of the input buffer. Keep the last line
37
51
in the buffer (useful when there are several commands on the same line). *)
38
-
39
52
let resynch_buffer ibuf =
53
+ let () = peek_to_newline ibuf in
40
54
match ibuf.bols with
41
55
| ll ::_ ->
42
56
let new_len = ibuf.len - ll in
@@ -90,7 +104,11 @@ let get_bols_of_loc ibuf (bp,ep) =
90
104
let nafter = if ll < ep then add_line (ll,ba) after else after in
91
105
lines_rec ll nafter fl
92
106
in
93
- let (fl,ll) = lines_rec ibuf.len ([] ,None ) ibuf.bols in
107
+ let bols = match ibuf.bols with
108
+ | [] -> [ibuf.len+ 1 ] (* no newline at the end of the command, pretend there was one *)
109
+ | _ :: _ as bols -> bols
110
+ in
111
+ let (fl,ll) = lines_rec ibuf.len ([] ,None ) bols in
94
112
(fl,Option. get ll)
95
113
96
114
let dotted_location (b ,e ) =
@@ -217,19 +235,23 @@ let top_buffer =
217
235
^ make_emacs_prompt doc
218
236
^ emacs_prompt_endstring()
219
237
in
238
+ let stream = Gramlib.Stream. empty () in
220
239
{ prompt = pr;
221
240
str = Bytes. empty;
222
241
len = 0 ;
223
242
bols = [] ;
224
- tokens = Pcoq.Parsable. make (Gramlib.Stream. empty () );
243
+ stream;
244
+ tokens = Pcoq.Parsable. make stream;
225
245
start = 0 }
226
246
227
247
(* Intialize or reinitialize the char stream *)
228
248
let reset_input_buffer ~state =
249
+ let stream = Gramlib.Stream. from (prompt_char state.Vernac.State. doc stdin top_buffer) in
229
250
top_buffer.str < - Bytes. empty;
230
251
top_buffer.len < - 0 ;
231
252
top_buffer.bols < - [] ;
232
- top_buffer.tokens < - Pcoq.Parsable. make (Gramlib.Stream. from (prompt_char state.Vernac.State. doc stdin top_buffer));
253
+ top_buffer.stream < - stream;
254
+ top_buffer.tokens < - Pcoq.Parsable. make stream;
233
255
top_buffer.start < - 0
234
256
235
257
let set_prompt prompt =
0 commit comments