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
open! Core
open! Import
let normalize_vars =
Funcall.Wrap.("comment-normalize-vars" <: nullary @-> return nil)
;;
module Vars = struct
let start = Var.Wrap.("comment-start" <: string)
let start_regexp = Var.Wrap.("comment-start-skip" <: Regexp.t)
let end_ = Var.Wrap.("comment-end" <: string)
let end_regexp = Var.Wrap.("comment-end-skip" <: Regexp.t)
let multi_line = Var.Wrap.("comment-multi-line" <: bool)
end
let wrap_var var () =
normalize_vars ();
Current_buffer.value_exn var
;;
let start = wrap_var Vars.start
let start_regexp = wrap_var Vars.start_regexp
let end_ = wrap_var Vars.end_
let end_regexp = wrap_var Vars.end_regexp
let multi_line = wrap_var Vars.multi_line
module Terminated_by = struct
type t =
| End_of_line
[@@deriving sexp_of]
let in_current_buffer () =
match end_ () with
| "" -> End_of_line
| _ -> Comment_end
;;
end
let set_current_buffer_options ~start:s ~end_:e ~is_multi_line:m =
Current_buffer.set_value Vars.start s;
Current_buffer.set_value Vars.end_ e;
Current_buffer.set_value Vars.multi_line m
;;
let beginning =
let =
Funcall.Wrap.("comment-beginning" <: nullary @-> return (nil_or Position.t))
in
fun () ->
normalize_vars ();
comment_beginning ()
;;
let () =
let eol =
Point.end_of_line ();
Point.get ()
in
Point.beginning_of_line ();
Point.search_forward_regexp ~bound:eol (Current_buffer.value_exn Vars.start_regexp)
;;
let () =
Current_buffer.save_excursion Sync (fun () ->
find_start_of_single_line_comment_at_point ())
;;
let () =
normalize_vars ();
match Terminated_by.in_current_buffer () with
| Comment_end ->
let search dir for_ =
let point = Point.get () in
Current_buffer.save_excursion Sync (fun () ->
Option.try_with (fun () ->
let check, search =
match dir with
| `Forward ->
Point.beginning_of_line ();
( (fun () -> Position.( <= ) (Point.get ()) point)
, fun () -> Point.search_forward_regexp_exn for_ )
| `Backward ->
Point.end_of_line ();
( (fun () -> Position.( >= ) (Point.get ()) point)
, fun () -> Point.search_backward_regexp_exn for_ )
in
while check () do
search ()
done;
Point.get ()))
in
let start_regexp = Current_buffer.value_exn Vars.start_regexp in
let end_regexp = Current_buffer.value_exn Vars.end_regexp in
(match
( search `Backward end_regexp
, search `Backward start_regexp
, search `Forward end_regexp
, search `Forward start_regexp )
with
| _, None, _, _ | _, _, None, _ ->
None
| Some previous_end, Some , Some , Some next_start
when Position.( >= ) previous_end comment_start
|| Position.( <= ) next_start comment_end ->
None
| _, Some , Some , _ ->
Some (comment_start, comment_end))
| End_of_line ->
(match am_in_single_line_comment () with
| false -> None
| true ->
let rec search dir ~last_point =
Point.forward_line
(match dir with
| `Backward -> -1
| `Forward -> 1);
match am_in_single_line_comment () with
| false -> last_point
| true ->
if Position.( <> ) last_point (Point.get ())
then search dir ~last_point:(Point.get ())
else Point.get ()
in
let =
Current_buffer.save_excursion Sync (fun () ->
Point.goto_char (search `Backward ~last_point:(Point.get ()));
ignore (find_start_of_single_line_comment_at_point () : bool);
Point.get ())
in
let =
Current_buffer.save_excursion Sync (fun () ->
Point.goto_char (search `Forward ~last_point:(Point.get ()));
Point.end_of_line ();
Point.get ())
in
Some (comment_start, comment_end))
;;
let goto_beginning_exn () =
let =
match bounds_of_comment_at_point () with
| None -> raise_s [%sexp "not in a comment"]
| Some (beginning, _) -> beginning
in
Point.goto_char comment_beginning;
try
let eol =
Current_buffer.save_excursion Sync (fun () ->
Point.end_of_line ();
Point.get ())
in
let search_forward () =
Point.search_forward_regexp_exn
~bound:eol
(Current_buffer.value_exn Vars.start_regexp)
in
try search_forward () with
| _ ->
Point.beginning_of_line ();
search_forward ()
with
| _ -> Point.goto_char comment_beginning
;;
let goto_end_exn =
let =
Funcall.Wrap.("comment-enter-backward" <: nullary @-> return nil)
in
fun () ->
let =
match bounds_of_comment_at_point () with
| None -> raise_s [%sexp "not in a comment"]
| Some (_, end_) -> end_
in
Point.goto_char comment_end;
comment_enter_backward ()
;;