package vcaml

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

Source file window.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
open Core
include Nvim_internal.Window

let get_height t = Nvim_internal.nvim_win_get_height ~window:t |> Api_call.of_api_result

let set_height t ~height =
  Nvim_internal.nvim_win_set_height ~window:t ~height |> Api_call.of_api_result
;;

let get_cursor t =
  let open Api_call.Let_syntax in
  let%map cursor =
    Nvim_internal.nvim_win_get_cursor ~window:t |> Api_call.of_api_result
  in
  let open Or_error.Let_syntax in
  match%bind cursor with
  | [ Msgpack.Integer row; Integer col ] -> Ok { Position.One_indexed_row.row; col }
  | _ -> Or_error.error_string "malformed result from [nvim_win_get_cursor]"
;;

let set_cursor t { Position.One_indexed_row.row; col } =
  let pos = [ Msgpack.Integer row; Integer col ] in
  Nvim_internal.nvim_win_set_cursor ~window:t ~pos |> Api_call.of_api_result
;;

module Untested = struct
  let get_buf t = Nvim_internal.nvim_win_get_buf ~window:t |> Api_call.of_api_result
  let get_width t = Nvim_internal.nvim_win_get_width ~window:t |> Api_call.of_api_result

  let set_width t ~width =
    Nvim_internal.nvim_win_set_width ~window:t ~width |> Api_call.of_api_result
  ;;

  let get_var t ~name ~type_ =
    Nvim_internal.nvim_win_get_var ~window:t ~name
    |> Api_call.of_api_result
    |> Api_call.map_bind ~f:(Extract.value type_)
  ;;

  let set_var t ~name ~type_ ~value =
    let value = Extract.inject type_ value in
    Nvim_internal.nvim_win_set_var ~window:t ~name ~value |> Api_call.of_api_result
  ;;

  let delete_var t ~name =
    Nvim_internal.nvim_win_del_var ~window:t ~name |> Api_call.of_api_result
  ;;

  let get_option t ~name ~type_ =
    Nvim_internal.nvim_win_get_option ~window:t ~name
    |> Api_call.of_api_result
    |> Api_call.map_bind ~f:(Extract.value type_)
  ;;

  let set_option t ~scope ~name ~type_ ~value =
    let vcaml_tmp = "__vcaml_tmp" in
    let value = Extract.inject type_ value in
    let command command = Nvim_internal.nvim_command ~command in
    let set_option = Nvim_internal.nvim_win_set_option ~window:t ~name ~value in
    let api_results =
      match scope with
      | `Global -> [ set_option ]
      | `Local ->
        [ command [%string "let %{vcaml_tmp} = &g:%{name}"]
        ; set_option
        ; command [%string "let &g:%{name} = %{vcaml_tmp}"]
        ; command [%string "unlet %{vcaml_tmp}"]
        ]
    in
    api_results |> List.map ~f:Api_call.of_api_result |> Api_call.Or_error.all_unit
  ;;

  let get_position t =
    let open Api_call.Let_syntax in
    let%map position =
      Nvim_internal.nvim_win_get_position ~window:t |> Api_call.of_api_result
    in
    let open Or_error.Let_syntax in
    match%bind position with
    | [ Msgpack.Integer row; Integer col ] -> Ok { Position.row; col }
    | _ -> Or_error.error_string "malformed result from [nvim_win_get_position]"
  ;;

  let get_tabpage t =
    Nvim_internal.nvim_win_get_tabpage ~window:t |> Api_call.of_api_result
  ;;

  let get_number t = Nvim_internal.nvim_win_get_number ~window:t |> Api_call.of_api_result
  let is_valid t = Nvim_internal.nvim_win_is_valid ~window:t |> Api_call.of_api_result

  let get_config t =
    Nvim_internal.nvim_win_get_config ~window:t
    |> Api_call.of_api_result
    |> Api_call.map_bind ~f:Extract.map_of_msgpack_alist
  ;;

  let set_config t ~config =
    let config = Extract.map_to_msgpack_alist config in
    Nvim_internal.nvim_win_set_config ~window:t ~config |> Api_call.of_api_result
  ;;

  let open_ ~buffer ~enter ~config =
    let config = Extract.map_to_msgpack_alist config in
    Nvim_internal.nvim_open_win ~buffer ~enter ~config
    |> Api_call.of_api_result
    |> Api_call.map ~f:(function
      | Error _ as error -> error
      | Ok t ->
        (match (t :> int) with
         | 0 ->
           Or_error.error_s
             [%message
               "nvim_open_win failed (returned 0)"
                 (buffer : Nvim_internal.Buffer.Or_current.t)]
         | _ -> Ok t))
  ;;

  module When_this_is_the_buffer's_last_window = struct
    type t =
      | Hide
      | Unload of { if_modified : [ `Hide | `Abort ] }
  end

  let close t ~when_this_is_the_buffer's_last_window =
    (match when_this_is_the_buffer's_last_window with
     | When_this_is_the_buffer's_last_window.Hide -> Nvim_internal.nvim_win_hide ~window:t
     | Unload { if_modified } ->
       let force =
         match if_modified with
         | `Hide -> true
         | `Abort -> false
       in
       Nvim_internal.nvim_win_close ~window:t ~force)
    |> Api_call.of_api_result
  ;;

  module Expert = struct
    let set_buf t ~buffer =
      Nvim_internal.nvim_win_set_buf ~window:t ~buffer |> Api_call.of_api_result
    ;;

    let win_call t ~lua_callback =
      Nvim_internal.nvim_win_call ~window:t ~fun_:lua_callback |> Api_call.of_api_result
    ;;
  end
end
OCaml

Innovation. Community. Security.