package ecaml

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file save_wrappers.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
open! Core
open! Import

module Q = struct
  include Q

  let save_current_buffer = "save-current-buffer" |> Symbol.intern
  let save_excursion = "save-excursion" |> Symbol.intern
  let save_mark_and_excursion = "save-mark-and-excursion" |> Symbol.intern
  let save_match_data = "save-match-data" |> Symbol.intern
  let save_restriction = "save-restriction" |> Symbol.intern
  let save_selected_window = "save-selected-window" |> Symbol.intern
  let save_window_excursion = "save-window-excursion" |> Symbol.intern
  let with_selected_frame = "with-selected-frame" |> Symbol.intern
  let with_selected_window = "with-selected-window" |> Symbol.intern
end

let save_sync save_function args f =
  let r = ref None in
  let f = Defun.lambda_nullary_nil [%here] (fun () -> r := Some (f ())) in
  ignore
    (Form.Blocking.eval
       (Form.list
          (List.concat
             [ [ save_function |> Form.symbol ]
             ; args |> List.map ~f:Form.of_value_exn
             ; [ Form.list
                   [ Q.funcall |> Form.symbol; f |> Function.to_value |> Form.quote ]
               ]
             ]))
      : Value.t);
  match !r with
  | None -> assert false
  | Some a -> a
;;

let save_
  (type a b)
  (sync_or_async : (a, b) Sync_or_async.t)
  save_function
  args
  (f : unit -> b)
  : b
  =
  match sync_or_async with
  | Sync -> save_sync save_function args f
  | Async ->
    Background.assert_foreground
      [%here]
      ~message:
        [%sexp
          (sprintf
             "%s called asynchronously in background job"
             (Symbol.name save_function)
            : string)];
    Value.Private.run_outside_async [%here] (fun () ->
      save_sync save_function args (fun () -> Value.Private.block_on_async [%here] f))
;;

let save_current_buffer sync_or_async f = save_ sync_or_async Q.save_current_buffer [] f
let save_excursion sync_or_async f = save_ sync_or_async Q.save_excursion [] f

let save_mark_and_excursion sync_or_async f =
  save_ sync_or_async Q.save_mark_and_excursion [] f
;;

let save_match_data sync_or_async f = save_ sync_or_async Q.save_match_data [] f
let save_restriction sync_or_async f = save_ sync_or_async Q.save_restriction [] f

let save_window_excursion sync_or_async f =
  save_ sync_or_async Q.save_window_excursion [] f
;;

let save_selected_window sync_or_async f = save_ sync_or_async Q.save_selected_window [] f

let with_selected_frame sync_or_async frame f =
  save_ sync_or_async Q.with_selected_frame [ frame ] f
;;

let with_selected_window sync_or_async window f =
  save_ sync_or_async Q.with_selected_window [ window ] f
;;