315 |
315 |
316 fun print_persistent (Print {persistent, ...}) = persistent; |
316 fun print_persistent (Print {persistent, ...}) = persistent; |
317 |
317 |
318 val overlay_ord = prod_ord string_ord (list_ord string_ord); |
318 val overlay_ord = prod_ord string_ord (list_ord string_ord); |
319 |
319 |
|
320 fun make_print (name, args) {delay, pri, persistent, strict, print_fn} eval = |
|
321 let |
|
322 val exec_id = Document_ID.make (); |
|
323 fun process () = |
|
324 let |
|
325 val {failed, command, state = st', ...} = eval_result eval; |
|
326 val tr = Toplevel.exec_id exec_id command; |
|
327 val opt_context = try Toplevel.generic_theory_of st'; |
|
328 in |
|
329 if failed andalso strict then () |
|
330 else print_error tr opt_context (fn () => print_fn tr st') |
|
331 end; |
|
332 in |
|
333 Print { |
|
334 name = name, args = args, delay = delay, pri = pri, persistent = persistent, |
|
335 exec_id = exec_id, print_process = Lazy.lazy_name "Command.print" process} |
|
336 end; |
|
337 |
|
338 fun bad_print name_args exn = |
|
339 make_print name_args {delay = NONE, pri = 0, persistent = false, |
|
340 strict = false, print_fn = fn _ => fn _ => Exn.reraise exn}; |
|
341 |
320 in |
342 in |
321 |
343 |
322 fun print command_visible command_overlays keywords command_name eval old_prints = |
344 fun print command_visible command_overlays keywords command_name eval old_prints = |
323 let |
345 let |
324 val print_functions = Synchronized.value print_functions; |
346 val print_functions = Synchronized.value print_functions; |
325 |
347 |
326 fun make_print name args {delay, pri, persistent, strict, print_fn} = |
348 fun new_print (name, args) get_pr = |
327 let |
|
328 val exec_id = Document_ID.make (); |
|
329 fun process () = |
|
330 let |
|
331 val {failed, command, state = st', ...} = eval_result eval; |
|
332 val tr = Toplevel.exec_id exec_id command; |
|
333 val opt_context = try Toplevel.generic_theory_of st'; |
|
334 in |
|
335 if failed andalso strict then () |
|
336 else print_error tr opt_context (fn () => print_fn tr st') |
|
337 end; |
|
338 in |
|
339 Print { |
|
340 name = name, args = args, delay = delay, pri = pri, persistent = persistent, |
|
341 exec_id = exec_id, print_process = Lazy.lazy_name "Command.print" process} |
|
342 end; |
|
343 |
|
344 fun bad_print name args exn = |
|
345 make_print name args {delay = NONE, pri = 0, persistent = false, |
|
346 strict = false, print_fn = fn _ => fn _ => Exn.reraise exn}; |
|
347 |
|
348 fun new_print name args get_pr = |
|
349 let |
349 let |
350 val params = |
350 val params = |
351 {keywords = keywords, |
351 {keywords = keywords, |
352 command_name = command_name, |
352 command_name = command_name, |
353 args = args, |
353 args = args, |
354 exec_id = eval_exec_id eval}; |
354 exec_id = eval_exec_id eval}; |
355 in |
355 in |
356 (case Exn.capture (Runtime.controlled_execution NONE get_pr) params of |
356 (case Exn.capture (Runtime.controlled_execution NONE get_pr) params of |
357 Exn.Res NONE => NONE |
357 Exn.Res NONE => NONE |
358 | Exn.Res (SOME pr) => SOME (make_print name args pr) |
358 | Exn.Res (SOME pr) => SOME (make_print (name, args) pr eval) |
359 | Exn.Exn exn => SOME (bad_print name args exn)) |
359 | Exn.Exn exn => SOME (bad_print (name, args) exn eval)) |
360 end; |
360 end; |
361 |
361 |
362 fun get_print (a, b) = |
362 fun get_print (name, args) = |
363 (case find_first (fn Print {name, args, ...} => name = a andalso args = b) old_prints of |
363 (case find_first (fn Print print => (#name print, #args print) = (name, args)) old_prints of |
364 NONE => |
364 NONE => |
365 (case AList.lookup (op =) print_functions a of |
365 (case AList.lookup (op =) print_functions name of |
366 NONE => SOME (bad_print a b (ERROR ("Missing print function " ^ quote a))) |
366 NONE => |
367 | SOME get_pr => new_print a b get_pr) |
367 SOME (bad_print (name, args) (ERROR ("Missing print function " ^ quote name)) eval) |
|
368 | SOME get_pr => new_print (name, args) get_pr) |
368 | some => some); |
369 | some => some); |
369 |
370 |
370 val new_prints = |
371 val new_prints = |
371 if command_visible then |
372 if command_visible then |
372 fold (fn (a, _) => cons (a, [])) print_functions command_overlays |
373 fold (fn (name, _) => cons (name, [])) print_functions command_overlays |
373 |> sort_distinct overlay_ord |
374 |> sort_distinct overlay_ord |
374 |> map_filter get_print |
375 |> map_filter get_print |
375 else filter (fn print => print_finished print andalso print_persistent print) old_prints; |
376 else filter (fn print => print_finished print andalso print_persistent print) old_prints; |
376 in |
377 in |
377 if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints |
378 if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints |