package ecaml

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

Source file customization.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
open! Core_kernel
open! Import
include Customization_intf

module Q = struct
  include Q

  let alist = "alist" |> Symbol.intern
  and boolean = "boolean" |> Symbol.intern
  and character = "character" |> Symbol.intern
  and choice = "choice" |> Symbol.intern
  and coding_system = "coding-system" |> Symbol.intern
  and color = "color" |> Symbol.intern
  and cons = "cons" |> Symbol.intern
  and const = "const" |> Symbol.intern
  and customize_group = "customize-group" |> Symbol.intern
  and customize_variable = "customize-variable" |> Symbol.intern
  and defcustom = "defcustom" |> Symbol.intern
  and defgroup = "defgroup" |> Symbol.intern
  and directory = "directory" |> Symbol.intern
  and float = "float" |> Symbol.intern
  and function_ = "function" |> Symbol.intern
  and group = "group" |> Symbol.intern
  and hook = "hook" |> Symbol.intern
  and integer = "integer" |> Symbol.intern
  and plist = "plist" |> Symbol.intern
  and radio = "radio" |> Symbol.intern
  and repeat = "repeat" |> Symbol.intern
  and string = "string" |> Symbol.intern
  and variable = "variable" |> Symbol.intern
end

module F = struct
  open Funcall

  let customize_group = Q.customize_group <: Symbol.type_ @-> return nil
  let customize_variable = Q.customize_variable <: Symbol.type_ @-> return nil
end

let customize_variable = F.customize_variable
let customize_group = F.customize_group
let q value = Value.list [ Symbol.to_value Q.quote; value ]

module Group = struct
  include (
    Symbol :
    sig
      type t = Symbol.t [@@deriving sexp_of]

      include Valueable.S with type t := t
    end)

  let all_defgroups = ref []

  let defgroup group_name here ~docstring ~parents =
    let form_of_parent parent =
      Form.[ Q.K.group |> symbol; parent |> Symbol.to_value |> quote ]
    in
    let docstring =
      sprintf "%s\n\nDefined at %s" docstring (here |> Source_code_position.to_string)
    in
    Form.(
      eval_i
        (list
           (List.concat
              [ [ Q.defgroup |> symbol; group_name |> symbol; nil; docstring |> string ]
              ; List.concat_map parents ~f:form_of_parent
              ])));
    all_defgroups := group_name :: !all_defgroups;
    group_name
  ;;

  let of_string = Symbol.intern
  let to_string = Symbol.name
  let to_symbol t = t
  let emacs = of_string "emacs"
end

module Type = struct
  type t =
    | Alist of t * t
    | Boolean
    | Character
    | Choice of t list
    | Coding_system
    | Color
    | Cons of t * t
    | Const of Value.t
    | Directory
    | Existing_file
    | Face
    | File
    | Float
    | Function
    | Group of t
    | Hook
    | Integer
    | List of t list
    | Number
    | Option of string * t
    | Plist
    | Radio of t list
    | Regexp
    | Repeat of t
    | Set of t list
    | Sexp
    | String
    | Symbol
    | Tagged_string of string
    | Variable
    | Vector of t list
  [@@deriving sexp_of]

  let s = Symbol.to_value

  let rec vs ts = List.map ts ~f:v
  and composite s ts = Value.list (Symbol.to_value s :: vs ts)

  and v = function
    | Alist (t1, t2) ->
      Value.list [ s Q.alist; s Q.K.key_type; v t1; s Q.K.value_type; v t2 ]
    | Boolean -> s Q.boolean
    | Character -> s Q.character
    | Choice ts -> composite Q.choice ts
    | Coding_system -> s Q.coding_system
    | Color -> s Q.color
    | Cons (t1, t2) -> composite Q.cons [ t1; t2 ]
    | Const v -> Value.list [ s Q.const; v ]
    | Directory -> s Q.directory
    | Existing_file -> Value.list [ s Q.file; s Q.K.must_match; Value.t ]
    | Face -> s Q.face
    | File -> s Q.file
    | Float -> s Q.float
    | Function -> s Q.function_
    | Group t -> composite Q.group [ t ]
    | Hook -> s Q.hook
    | Integer -> s Q.integer
    | List ts -> composite Q.list ts
    | Number -> s Q.number
    | Option (none, t) ->
      Value.list
        [ s Q.choice
        ; Value.list [ s Q.const; s Q.K.tag; Value.of_utf8_bytes none; s Q.nil ]
        ; v t
        ]
    | Plist -> s Q.plist
    | Radio ts -> composite Q.radio ts
    | Regexp -> s Q.regexp
    | Repeat t -> composite Q.repeat [ t ]
    | Set ts -> composite Q.set ts
    | Sexp -> s Q.sexp
    | String -> s Q.string
    | Symbol -> s Q.symbol
    | Tagged_string tag -> Value.list [ v String; s Q.K.tag; Value.of_utf8_bytes tag ]
    | Variable -> s Q.variable
    | Vector ts -> composite Q.vector ts
  ;;

  let to_value = v
  let enum all value_of_a = Choice (List.map all ~f:(fun a -> Const (value_of_a a)))
end

let all_defcustom_symbols = ref []

module Private = struct
  let all_defcustom_symbols () =
    !all_defcustom_symbols |> List.sort ~compare:Symbol.compare_name
  ;;

  let all_defgroups () = !Group.all_defgroups |> List.sort ~compare:Symbol.compare_name
end

let defcustom
      ?(show_form = false)
      symbol
      here
      ~docstring
      ~group
      ~type_
      ~customization_type
      ~standard_value
      ()
  =
  let standard_value = standard_value |> Value.Type.to_value type_ in
  (try
     let docstring =
       concat
         [ docstring |> String.strip
         ; "\n\n"
         ; concat [ "Customization group: "; group |> Group.to_string; "\n" ]
         ; concat
             [ "Customization type:"
             ; (let string =
                  [%sexp (customization_type : Type.t)] |> Sexp.to_string_hum
                in
                if String.contains string '\n'
                then concat [ "\n"; string ]
                else concat [ " "; string ])
             ]
         ]
     in
     all_defcustom_symbols := symbol :: !all_defcustom_symbols;
     Load_history.add_entry here (Var symbol);
     let form =
       [ Q.defcustom |> Symbol.to_value
       ; symbol |> Symbol.to_value
       ; standard_value |> q
       ; docstring |> Value.of_utf8_bytes
       ; Q.K.group |> Symbol.to_value
       ; group |> Symbol.to_value |> q
       ; Q.K.type_ |> Symbol.to_value
       ; customization_type |> Type.to_value |> q
       ]
       |> Value.list
       |> Form.of_value_exn
     in
     if show_form then Echo_area.message_s [%sexp (form : Form.t)];
     ignore (Form.eval form : Value.t)
   with
   | exn ->
     raise_s
       [%message
         "[defcustom] failed"
           (exn : exn)
           (symbol : Symbol.t)
           (customization_type : Type.t)
           (group : Symbol.t)
           (standard_value : Value.t)
           (docstring : string)
           ~_:(here : Source_code_position.t)]);
  Var.create symbol type_
;;

module Enum = struct
  module type Arg = Enum_arg
  module type S = Enum

  let make (type t) symbol here (module T : Arg with type t = t) ~docstring ~group =
    ( module struct
      type t = T.t

      let ({ Value.Type.of_value_exn; to_value; id = _ } as type_) =
        Value.Type.enum
          [%sexp (Symbol.name symbol : string)]
          (module T)
          (T.to_symbol >> Symbol.to_value)
      ;;

      let var = Var.create symbol type_

      let docstring =
        concat
          ~sep:"\n"
          (docstring
           :: ""
           :: List.map T.all ~f:(fun t ->
             let docstring =
               match T.docstring t with
               | "" -> []
               | docstring -> [ ": "; docstring ]
             in
             concat ("  - " :: (t |> T.to_symbol |> Symbol.name) :: docstring)))
      ;;

      let initialize_defcustom () =
        ignore
          ( defcustom
              symbol
              here
              ~docstring
              ~group
              ~type_
              ~customization_type:(Type.enum T.all to_value)
              ~standard_value:T.standard_value
              ()
            : _ Var.t )
      ;;
    end
    : S
      with type t = t )
  ;;
end