Source file tabulated_list.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
open! Core
open! Import
module Column = struct
module Format = struct
type 'a t =
{ align_right : bool
; : string
; pad_right : int
; sortable : bool
; width : 'a
}
[@@deriving sexp_of]
let create
?(align_right = false)
?(pad_right = 1)
?(sortable = true)
~
~width
()
=
{ align_right; pad_right; sortable; header; width }
;;
module Fixed_width = struct
type nonrec t = int t [@@deriving sexp_of]
module Property = struct
type t =
| Align_right of bool
| Pad_right of int
let to_values = function
| Align_right b -> [ Q.K.right_align |> Symbol.to_value; b |> Value.of_bool ]
| Pad_right padding ->
[ Q.K.pad_right |> Symbol.to_value; padding |> Value.of_int_exn ]
;;
let of_values (keyword, value) =
if Value.equal (Q.K.right_align |> Symbol.to_value) keyword
then Align_right (Value.to_bool value)
else if Value.equal (Q.K.pad_right |> Symbol.to_value) keyword
then Pad_right (Value.to_int_exn value)
else
raise_s
[%sexp "Invalid Property keyword", (keyword : Value.t), (value : Value.t)]
;;
end
module Properties = struct
let rec pairs = function
| [] -> []
| [ _ ] -> raise_s [%sexp "Received an odd number of list elements"]
| a :: b :: rest -> (a, b) :: pairs rest
;;
let type_ =
let open Value.Type in
map
(list value)
~name:[%sexp "tabulated-list-column-format-property-list"]
~of_:(fun x -> List.map (pairs x) ~f:Property.of_values)
~to_:(List.concat_map ~f:Property.to_values)
;;
let t = type_
end
let type_ =
let format = create in
let open Value.Type in
map
(tuple string (tuple int (tuple bool Properties.t)))
~name:[%sexp "Column.Format"]
~of_:(fun (, (width, (sortable, props))) ->
let t = format ~header ~sortable ~width () in
List.fold props ~init:t ~f:(fun t -> function
| Align_right align_right -> { t with align_right }
| Pad_right pad_right -> { t with pad_right }))
~to_:(fun { align_right; ; pad_right; sortable; width } ->
header, (width, (sortable, [ Align_right align_right; Pad_right pad_right ])))
;;
let t = type_
end
end
module Variable_width = struct
type t =
{ max_width : int option
; min_width : int
}
[@@deriving compare, fields, sexp_of]
let create = Fields.create
let to_fixed_width t values =
let width =
List.map values ~f:Text.length
|> List.max_elt ~compare:[%compare: int]
|> Option.value ~default:0
in
Option.fold t.max_width ~init:width ~f:Int.min |> Int.max t.min_width
;;
end
type 'record t =
{ field_of_record : 'record -> Text.t
; format : Variable_width.t Format.t
}
[@@deriving fields]
let text ?align_right ?max_width ?min_width ?pad_right ?sortable ~ field_of_record
=
{ field_of_record
; format =
(let min_width = Option.fold min_width ~init:(String.length header) ~f:Int.max in
let width = Variable_width.create ~max_width ~min_width in
Format.create ?align_right ?pad_right ?sortable ~header ~width ())
}
;;
let create
?align_right
?max_width
?min_width
?pad_right
?sortable
~
field_of_record
=
text
?align_right
?max_width
?min_width
?pad_right
?sortable
~header
(field_of_record >> Text.of_utf8_bytes)
;;
let fixed_width_format t values =
{ t.format with
width =
List.map values ~f:t.field_of_record
|> Variable_width.to_fixed_width t.format.width
}
;;
let first_line
?align_right
?max_width
?min_width
?pad_right
?sortable
~
field_of_record
=
create ?align_right ?max_width ?min_width ?pad_right ?sortable ~header (fun record ->
let str = field_of_record record in
(match String.split_lines str with
| line :: _ :: _ -> sprintf "%s..." (String.rstrip line)
| [] | [ _ ] -> str)
|> String.strip)
;;
end
type ('record, 'id) t =
{ columns : 'record Column.t list
; entries_var : 'record list Buffer_local.t
; id_equal : 'id -> 'id -> bool
; id_of_record : 'record -> 'id
; id_type : 'id Value.Type.t
; major_mode : Major_mode.t
}
[@@deriving fields]
let keymap t = Major_mode.keymap (major_mode t)
let tabulated_list_format_var =
Buffer_local.Wrap.("tabulated-list-format" <: vector Column.Format.Fixed_width.t)
;;
let tabulated_list_sort_key_var =
Buffer_local.Wrap.("tabulated-list-sort-key" <: nil_or (tuple string bool))
;;
let =
Funcall.Wrap.("tabulated-list-init-header" <: nullary @-> return nil)
;;
let tabulated_list_print =
Funcall.Wrap.("tabulated-list-print" <: bool @-> bool @-> return nil)
;;
let draw ?sort_by t rows =
Option.iter sort_by ~f:(fun (, _) ->
match
List.find t.columns ~f:(fun column -> String.equal sort_header column.format.header)
with
| None -> raise_s [%sexp "Unknown header to sort by", (sort_header : string)]
| Some column ->
if not column.format.sortable
then raise_s [%sexp "Column is not sortable", (sort_header : string)]);
Current_buffer.set_buffer_local
tabulated_list_sort_key_var
(Option.map
sort_by
~f:
(Tuple2.map_snd ~f:(function
| `Ascending -> false
| `Descending -> true)));
Current_buffer.set_buffer_local t.entries_var rows;
Current_buffer.set_buffer_local
tabulated_list_format_var
(Array.of_list
(List.map t.columns ~f:(fun column -> Column.fixed_width_format column rows)));
tabulated_list_init_header ();
tabulated_list_print true false
;;
module Tabulated_list_mode = (val Major_mode.wrap_existing "tabulated-list-mode" [%here])
let create major_mode columns ~id_equal ~id_type ~id_of_record =
if not (Major_mode.is_derived major_mode ~from:Tabulated_list_mode.major_mode)
then
raise_s
[%sexp
"[Tabulated_list.create] called on a major mode not derived from \
[Tabulated_list.Tabulated_list_mode.major_mode]."];
let entries_var =
let entry_type =
let open Value.Type in
map
(tuple id_type (tuple (vector Text.t) unit))
~name:[%message "tabulated-list-entries" (id_type : _ Value.Type.t)]
~of_:(fun _ -> raise_s [%sexp "reading tabulated-list-entries is not supported"])
~to_:(fun record ->
( id_of_record record
, ( columns
|> List.map ~f:(fun (column : _ Column.t) -> column.field_of_record record)
|> Array.of_list
, () ) ))
in
Buffer_local.Wrap.("tabulated-list-entries" <: list entry_type)
in
{ columns; entries_var; id_equal; id_of_record; id_type; major_mode }
;;
let tabulated_list_get_id =
Funcall.Wrap.("tabulated-list-get-id" <: nullary @-> return (nil_or value))
;;
let get_id_at_point_exn t =
Option.map (tabulated_list_get_id ()) ~f:(Value.Type.of_value_exn t.id_type)
;;
let move_point_to_id t id =
Point.goto_min ();
let rec loop () =
match get_id_at_point_exn t with
| None ->
raise_s [%sexp "Could not find row with given id"]
| Some id_at_point ->
(match t.id_equal id id_at_point with
| false ->
Point.forward_line 1;
loop ()
| true -> ())
in
loop ()
;;
let current_buffer_has_entries () =
not
(Value.is_nil
(Current_buffer.get_buffer_local
Buffer_local.Wrap.("tabulated-list-entries" <: value)))
;;
let revert_hook = Hook.Wrap.("tabulated-list-revert-hook" <: Normal_hook)