package ecaml

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

Source file command.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
open! Core_kernel
open! Import0

module Q = struct
  include Q

  let call_interactively = "call-interactively" |> Symbol.intern
  and current_prefix_arg = "current-prefix-arg" |> Symbol.intern
  and prefix_numeric_value = "prefix-numeric-value" |> Symbol.intern
end

module Current_buffer = Current_buffer0

include Value.Make_subtype (struct
    let name = "command"
    let here = [%here]
    let is_in_subtype = Value.is_command
  end)

module Raw_prefix_argument = struct
  type t =
    | Absent
    | Int of int
    | Minus
    | Nested of int
  [@@deriving sexp_of]

  let minus = "-" |> Value.intern

  let to_value = function
    | Absent -> Value.nil
    | Int i -> i |> Value.of_int_exn
    | Minus -> minus
    | Nested i -> Value.cons (i |> Value.of_int_exn) Value.nil
  ;;

  let of_value_exn value =
    if Value.is_nil value
    then Absent
    else if Value.is_integer value
    then Int (Value.to_int_exn value)
    else if Value.is_cons value
    then Nested (Value.car_exn value |> Value.to_int_exn)
    else if Value.eq value minus
    then Minus
    else
      raise_s
        [%message
          "[Raw_prefix_argument.of_value] got unexpected value" (value : Value.t)]
  ;;

  let type_ =
    Value.Type.create [%message "raw_prefix_arg"] [%sexp_of: t] of_value_exn to_value
  ;;

  let for_current_command = Var.create Q.current_prefix_arg type_

  let numeric_value t =
    Symbol.funcall1 Q.prefix_numeric_value (t |> to_value) |> Value.to_int_exn
  ;;
end

let call_interactively value raw_prefix_argument =
  Current_buffer.set_value Raw_prefix_argument.for_current_command raw_prefix_argument;
  Symbol.funcall1_i Q.call_interactively value
;;

let inhibit_quit = Var.create ("inhibit-quit" |> Symbol.intern) Value.Type.bool
let quit_flag = Var.create ("quit-flag" |> Symbol.intern) Value.Type.bool
let request_quit () = Current_buffer.set_value quit_flag true

let quit_requested () =
  (* We use [try-with] because calling into Elisp can itself check [quit-flag]
     and raise.  And in fact does, at least in Emacs 25.2. *)
  try Current_buffer.value_exn quit_flag with
  | _ -> true
;;