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
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 () =
Current_buffer.save_excursion Sync (fun () ->
Point.end_of_line ();
Option.is_some (beginning ()))
;;
module Syntax = struct
let = Funcall.Wrap.("forward-comment" <: int @-> return bool)
let syntax_ppss = Funcall.Wrap.("syntax-ppss" <: nullary @-> return value)
let =
Funcall.Wrap.("ppss-comment-or-string-start" <: value @-> return (nil_or Position.t))
;;
let () = ppss_comment_or_string_start (syntax_ppss ())
let () =
let%bind.Option = syntax_ppss_comment_start () in
let%bind.Option =
Current_buffer.save_excursion Sync (fun () ->
Point.goto_char comment_start;
if forward_comment 1 then Some (Point.get ()) else None)
in
Some (comment_start, comment_end)
;;
end
let () =
normalize_vars ();
match Terminated_by.in_current_buffer () with
| Comment_end ->
Syntax.bounds_of_comment_at_point ()
| 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 ()));
Point.end_of_line ();
match beginning () with
| Some pos -> pos
| None -> 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 ()
;;