package ecaml

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

Source file completing.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
open! Core
open! Async_kernel
open! Import
include (val Symbol_prefix.extend symbol_prefix "completing")

module Q = struct
  include Q

  let confirm = "confirm" |> Symbol.intern
  let confirm_after_completion = "confirm-after-completion" |> Symbol.intern
end

module Initial_input = struct
  type t =
    | Empty
    | Point_at_end of string
    | Point_at_pos of string * int
  [@@deriving sexp_of]

  let to_value = function
    | Empty -> Symbol.to_value Q.nil
    | Point_at_end s -> Value.of_utf8_bytes s
    | Point_at_pos (s, i) -> Value.cons (Value.of_utf8_bytes s) (Value.of_int_exn i)
  ;;

  let of_value_exn value =
    List.find_map_exn
      ~f:(fun f -> f value)
      [ (fun value ->
          match Symbol.equal (Symbol.of_value_exn value) Q.nil with
          | true -> Some Empty
          | false -> None
          | exception _ -> None)
      ; (fun value ->
           match Value.to_utf8_bytes_exn value with
           | s -> Some (Point_at_end s)
           | exception _ -> None)
      ; (fun value ->
           match Value.Type.(tuple string int |> of_value_exn) value with
           | s, i -> Some (Point_at_pos (s, i))
           | exception _ -> None)
      ]
  ;;

  let type_ =
    Value.Type.create
      [%sexp "completing", "initial-input"]
      [%sexp_of: t]
      of_value_exn
      to_value
  ;;

  let t = type_
end

module Require_match = struct
  type t =
    | Confirm
    | Confirm_after_completion
    | False
    | Require_match_or_null
    | True

  let type_ =
    Value.Type.map
      Symbol.t
      ~name:[%sexp "completing", "require-match"]
      ~of_:(fun symbol ->
        List.Assoc.find
          [ Q.confirm, Confirm
          ; Q.confirm_after_completion, Confirm_after_completion
          ; Q.nil, False
          ; Q.t, True
          ]
          symbol
          ~equal:Symbol.equal
        |> Option.value ~default:Require_match_or_null)
      ~to_:(function
        | Confirm -> Q.confirm
        | Confirm_after_completion -> Q.confirm_after_completion
        | False -> Q.nil
        | Require_match_or_null -> Symbol.intern "other"
        | True -> Q.t)
  ;;

  let t = type_
  let of_value_exn = Value.Type.of_value_exn type_
  let to_value = Value.Type.to_value type_
  let default = False
end

module Collection = (val Ocaml_or_elisp_value.make Value.Type.(list string_cached))

module Programmed_completion = struct
  include (val Symbol_prefix.extend symbol_prefix "programmed-completion")

  module Operation = struct
    include (val Symbol_prefix.extend symbol_prefix "operation")

    type t =
      | Metadata
      | Other of Value.t
    [@@deriving sexp_of]

    include Valueable.Make (struct
        type nonrec t = t

        let type_ =
          let metadata_value = Symbol.intern "metadata" |> Symbol.to_value in
          Value.Type.create
            [%message elisp_name]
            [%sexp_of: t]
            (fun value ->
               match Value.equal value metadata_value with
               | true -> Metadata
               | false -> Other value)
            (function
              | Metadata -> metadata_value
              | Other value -> value)
        ;;
      end)
  end

  module Metadata = struct
    type t =
      { annotation_function : (string -> string) option
      ; display_sort_function : (string list -> string list) option
      }
    [@@deriving fields]

    let create = Fields.create

    let exists =
      let f _field _t option = Option.is_some option in
      Fields.Direct.exists ~annotation_function:f ~display_sort_function:f
    ;;

    let to_value =
      let metadatum_type = Value.Type.(tuple Symbol.t Function.t) in
      let f ~arg ~returns field _t f =
        let%map.Option f = f in
        let name = Enum.Single.to_string_hum (module String) (Field.name field) in
        let function_ =
          Defun.lambda
            [%here]
            (Returns returns)
            (let%map_open.Defun arg = arg in
             f arg)
        in
        Value.Type.to_value metadatum_type (Symbol.intern name, function_)
      in
      Fields.Direct.to_list
        ~annotation_function:
          (f ~arg:Defun.(required "CANDIDATE" string) ~returns:Value.Type.string)
        ~display_sort_function:
          (f
             ~arg:Defun.(required "CANDIDATES" (list string))
             ~returns:Value.Type.(list string))
      >> List.filter_opt
      >> List.cons (Operation.to_value Metadata)
      >> Value.list
    ;;
  end

  type t =
    | Abstract_collection of Collection.abstract
    | Symbol of Symbol.t
    | T of
        { collection : string list
        ; metadata : Metadata.t
        }

  let completion_table_dynamic =
    Funcall.Wrap.("completion-table-dynamic" <: Function.t @-> return Function.t)
  ;;

  let to_value = function
    | Abstract_collection collection -> Collection.to_value (Elisp collection)
    | Symbol symbol -> Symbol.function_exn symbol
    | T { collection; metadata } ->
      (* Using the full "Programmed Completion" interface is necessary to provide
         annotation/sorting functions, but Emacs provides [completion_table_dynamic] to
         create a basic implementation of the interface, which we can delegate to for
         everything except adding our metadata. *)
      let dynamic =
        completion_table_dynamic
          (Defun.lambda
             [%here]
             (Returns Value.Type.(list string))
             (let%map_open.Defun () = required "PREFIX" ignored in
              collection))
      in
      Defun.lambda
        [%here]
        (Returns Value.Type.value)
        (let%map_open.Defun () = return ()
         and prefix = required "PREFIX" value
         and predicate = required "PREDICATE" value
         and operation = required "OPERATION" Operation.t in
         match operation with
         | Metadata -> Metadata.to_value metadata
         | Other operation ->
           Value.funcall3 (Function.to_value dynamic) prefix predicate operation)
      |> Function.to_value
  ;;

  let create (collection : Collection.t) ~annotation_function ~display_sort_function =
    let metadata = Metadata.create ~annotation_function ~display_sort_function in
    match collection with
    | This collection -> T { collection; metadata }
    | Elisp abstract ->
      (match Metadata.exists metadata with
       | false -> Abstract_collection abstract
       | true ->
         (* Not sure how to make [display_sort_function] work with the abstract
            collection. Let's not work on this until someone needs it. *)
         let s = "Programmed completion not supported with [Elisp _]." in
         raise_s
           [%message
             s (annotation_function : _ option) (display_sort_function : _ option)])
  ;;
end

let completing_read =
  let completing_read =
    Funcall.Wrap.(
      "completing-read"
      <: string
         @-> value
         @-> value
         @-> Require_match.t
         @-> Initial_input.t
         @-> Symbol.t
         @-> nil_or string
         @-> return string)
  in
  fun ~prompt
    ~programmed_completion
    ?(predicate = Value.nil)
    ?(require_match = Require_match.default)
    ?(initial_input = Initial_input.Empty)
    ?default
    ~history
    () ->
    Async_ecaml.Private.run_outside_async [%here] (fun () ->
      let prompt =
        match default with
        | None -> prompt
        | Some d -> concat [ prompt; "(default = "; d; ") " ]
      in
      completing_read
        prompt
        (Programmed_completion.to_value programmed_completion)
        predicate
        require_match
        initial_input
        (Minibuffer.History.symbol history)
        default)
;;

let read
      ~prompt
      ~collection
      ?annotation_function
      ?display_sort_function
      ?require_match
      ?initial_input
      ?default
      ~history
      ()
  =
  completing_read
    ~prompt
    ~programmed_completion:
      (Programmed_completion.create
         collection
         ~annotation_function
         ~display_sort_function)
    ?require_match
    ?initial_input
    ?default
    ~history
    ()
;;

let read_map_key
      ~prompt
      ~collection
      ?annotation_function
      ?display_sort_function
      ?initial_input
      ?default
      ~history
      ()
  =
  let%bind choice =
    read
      ~prompt
      ~collection:(This (Map.keys collection))
      ~require_match:True
      ?annotation_function
      ?display_sort_function
      ?initial_input
      ?default
      ~history
      ()
  in
  return (Map.find_exn collection choice)
;;

let crm_separator = Var.Wrap.("crm-separator" <: string)

let read_multiple =
  let completing_read_multiple =
    Funcall.Wrap.(
      "completing-read-multiple"
      <: string
         @-> value
         @-> value
         @-> Require_match.t
         @-> Initial_input.t
         @-> Symbol.t
         @-> nil_or string
         @-> return (list string))
  in
  fun ~prompt
    ~collection
    ?(require_match = Require_match.False)
    ?(separator_regexp = "[ \t]*,[ \t]*")
    ?(initial_input = Initial_input.Empty)
    ?default
    ~history
    () ->
    Async_ecaml.Private.run_outside_async [%here] (fun () ->
      let predicate = Value.nil in
      let prompt =
        match default with
        | None -> prompt
        | Some d -> concat [ prompt; "(default = "; d; ") " ]
      in
      Current_buffer.set_value_temporarily
        Sync
        crm_separator
        separator_regexp
        ~f:(fun () ->
          completing_read_multiple
            prompt
            (collection |> Collection.to_value)
            predicate
            require_match
            initial_input
            (Minibuffer.History.symbol history)
            default))
;;

let read_multiple_map_keys
      ~prompt
      ~collection
      ?separator_regexp
      ?initial_input
      ?default
      ~history
      ()
  =
  let%bind choices =
    read_multiple
      ~prompt
      ~collection:(This (Map.keys collection))
      ~require_match:True
      ?separator_regexp
      ?initial_input
      ?default
      ~history
      ()
  in
  return (List.map choices ~f:(Map.find_exn collection))
;;

let symbol_collection =
  lazy
    (Feature.require ("help-fns" |> Symbol.intern);
     "help--symbol-completion-table" |> Symbol.intern)
;;

let read_function_name =
  (* The code below is based on [describe-function]'s [interactive] spec. *)
  let predicate =
    lazy
      (Defun.lambda
         [%here]
         (Returns Value.Type.bool)
         (let%map_open.Defun () = return ()
          and f = required "f" Symbol.t in
          Symbol.function_is_defined f
          || is_some (Symbol.Property.get Symbol.Property.function_documentation f))
       |> Function.to_value)
  in
  fun ~prompt ~history ->
    let collection = force symbol_collection in
    let predicate = force predicate in
    let%bind name =
      completing_read
        ~prompt
        ~programmed_completion:(Symbol collection)
        ~predicate
        ~require_match:True
        ?default:(Point.function_called_at () |> Option.map ~f:Symbol.name)
        ~history
        ()
    in
    if String.is_empty name then raise_s [%message "Did not enter a function name"];
    return name
;;

let read_variable_name =
  let predicate =
    lazy
      (Defun.lambda
         [%here]
         (Returns Value.Type.bool)
         (let%map_open.Defun () = return ()
          and var = required "var" Symbol.t in
          Current_buffer.variable_is_defined var
          || is_some (Symbol.Property.get Symbol.Property.variable_documentation var))
       |> Function.to_value)
  in
  fun ~prompt ~history ->
    let collection = force symbol_collection in
    let predicate = force predicate in
    let%bind name =
      completing_read
        ~prompt
        ~programmed_completion:(Symbol collection)
        ~predicate
        ~require_match:True
        ?default:(Point.variable_at () |> Option.map ~f:Symbol.name)
        ~history
        ()
    in
    if String.is_empty name then raise_s [%message "Did not enter a variable name"];
    return name
;;