aboutsummaryrefslogtreecommitdiff
path: root/src/main.ml
blob: 5da7b90dc999b81aff7807242d76601e7495a805 (plain)
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
(*
This file is part of licht.

licht is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

licht is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with licht.  If not, see <http://www.gnu.org/licenses/>.
*)

module E:Sym_expr.SYM_EXPR = Evaluate

let u = UTF8.from_utf8string

let redraw t screen selection =
  Screen.draw t selection screen;
  Screen.draw_input t selection screen

let action f msg (t, screen, selection) = begin
  let count = f t in
  redraw t screen selection;
  Screen.status screen @@ UTF8.from_utf8string (Printf.sprintf msg count);
  t, screen, selection
end

let catalog = Functions.C.compile @@ Functions.built_in Functions.C.empty

let f screen = ActionParser.(
  begin match Screen.read_key screen with
  | "\027"      -> ESC
  | "\001\002"  -> DOWN
  | "\001\003"  -> UP
  | "\001\004"  -> LEFT
  | "\001\005"  -> RIGHT
  | "\001\006"  -> HOME
  | "\001\104"  -> END

  | "\001R"     -> NPAGE
  | "\001S"     -> PPAGE

    (* See http://www.ibb.net/~anne/keyboard.html for thoses keycode. *)
  | "\001\074"|"\127" -> DELETE

  | "e"         -> E
  | "u"         -> U
  | "v"         -> V
  | "y"         -> Y
  | "p"         -> P
  | "="         -> EQUAL
  | "/"         -> SEARCH
  | ":"         -> COMMAND

  | "\001\154"  -> RESIZE
  | "\001\153"  ->
    begin match Tools.NCurses.get_mouse_event () with
    | None -> raise Not_found
    | Some (id, ev, (x, y, z)) ->
        if Tools.NCurses.is_event_of_type Tools.NCurses.BUTTON1_CLICKED ev then
          BUTTON1_CLICKED(x, y)
        else if Tools.NCurses.is_event_of_type Tools.NCurses.BUTTON1_PRESSED ev then
          BUTTON1_CLICKED(x, y)
        else if Tools.NCurses.is_event_of_type Tools.NCurses.BUTTON1_RELEASED ev then
          BUTTON1_RELEASED(x, y)
        else
          raise Not_found
    end
  | _ -> raise Not_found
end)

let menhirParser =
  MenhirLib.Convert.Simplified.traditional2revised ActionParser.normal

let parser screen = begin
  let get_value () = f screen, Lexing.dummy_pos, Lexing.dummy_pos in
  menhirParser get_value
end

let rec normal_mode (t, screen, selection) = begin
  match (parser screen) with
  | exception x -> normal_mode (t, screen, selection)

  | Actions.Visual ->
      Screen.status screen @@ u"-- Selection --";
      selection_mode (t, screen, selection)

  | Actions.Move direction ->
      begin match Selection.move direction selection with
      | Some selection ->
          redraw t screen selection;
          normal_mode (t, screen, selection)
      | None -> normal_mode (t, screen, selection)
      end

  | Actions.Resize -> normal_mode (t, Screen.resize t selection screen, selection)

  | Actions.Delete ->
      let yank_before_delete = fun x ->
        ignore @@ Sheet.yank selection x;
        Sheet.delete selection x in
      normal_mode @@ action yank_before_delete "Deleted %d cells" (t, screen, selection)

  | Actions.Yank ->
      normal_mode @@ action (Sheet.yank selection) "Yanked %d cells" (t, screen, selection)

  | Actions.Paste ->
      let paste = Sheet.paste @@ Selection.extract selection in
      normal_mode @@ action paste "Pasted %d cells" (t, screen, selection)

  | Actions.Undo ->
      begin match Sheet.undo t with
      | true ->
        redraw t screen selection;
        normal_mode (t, screen, selection)
      | false -> normal_mode (t, screen, selection)
      end

  (* Edit a content *)
  | Actions.Edit ->
    let position = Selection.extract selection in
    let expr, _, _ = Sheet.get_cell position t in
    let expr_repr = Expression.show expr in
    begin match Screen.editor ~position ~prefix:(u"-> ") ~init:expr_repr screen with
    | None ->
      (* Restore the previous value *)
      Screen.status screen expr_repr;
      normal_mode (t, screen, selection)
    | Some content ->
      let expr' = Expression.load content in
      ignore @@ Sheet.add ~history:true expr' position t;
      redraw t screen selection;
      normal_mode (t, screen, selection)
    end

  (* Insert a new formula, the actual value will be erased *)
  | Actions.InsertFormula ->
    let position = Selection.extract selection in
    begin match Screen.editor ~position ~init:(u"=") screen with
    | None ->
      (* Restore the previous value *)
      let expr, _, _ = Sheet.get_cell position t in
      Screen.status screen (Expression.show expr);
      normal_mode (t, screen, selection)
    | Some content ->
      let expr = Expression.load content in
      ignore @@ Sheet.add ~history:true expr position t;
      redraw t screen selection;
      normal_mode (t, screen, selection)
    end

  | Actions.Button1_clicked coord ->
    begin match Screen.get_cell screen coord with
    | None -> normal_mode (t, screen, selection)
    | Some (x,y) -> begin match Selection.move (Actions.Absolute (x,y)) selection with
        | Some selection' ->
            redraw t screen selection';
            normal_mode (t, screen, selection')
        | None -> normal_mode (t, screen, selection)
      end
    end

  | Actions.Button1_released coord ->
    begin match Screen.get_cell screen coord with
    | Some (x,y) when (x,y) <> (Selection.extract selection) ->
      Screen.status screen @@ u"-- Selection -- ";
      let selection = Selection.extends (Actions.Absolute (x,y)) selection in
      Screen.draw t selection screen;
      selection_mode (t, screen, selection)
    | _ -> normal_mode (t, screen, selection)
    end

  | Actions.Command ->
    begin match Screen.editor ~prefix:(u":") screen with
    | None ->
      normal_mode (t, screen, selection)
    | Some content ->
        let args = try
          UTF8.to_utf8string content
          |> Tools.String.split ~by:' '
        with Not_found ->
          (UTF8.to_utf8string content, "") in
        command (t, screen, selection) args
    end

  | _ -> normal_mode (t, screen, selection)

end

and selection_mode (t, screen, selection) = begin
  match (parser screen) with
  | exception x -> selection_mode (t, screen, selection)

  | Actions.Resize -> selection_mode (t, Screen.resize t selection screen, selection)

  | Actions.Delete ->
    let yank_before_delete = fun x ->
      ignore @@ Sheet.yank selection x;
      Sheet.delete selection x in
    normal_mode @@ action yank_before_delete "Deleted %d cells" (t, screen, selection)

  | Actions.Yank ->
    normal_mode @@ action (Sheet.yank selection) "Yanked %d cells" (t, screen, selection)

  | Actions.Escape ->
    let selection = Selection.create (Selection.extract selection) in
    Screen.draw t selection screen;
    Screen.draw_input t selection screen;
    Screen.status screen UTF8.empty;
    normal_mode (t, screen, selection)

  | Actions.Move m ->
    let selection = Selection.extends m selection in
    Screen.draw t selection screen;
    selection_mode (t, screen, selection)

  | Actions.Button1_clicked coord ->
    begin match Screen.get_cell screen coord with
    | None -> normal_mode (t, screen, selection)
    | Some (x,y) -> begin match Selection.move (Actions.Absolute (x,y)) selection with
      | Some selection ->
          Screen.status screen UTF8.empty;
          redraw t screen selection;
          normal_mode (t, screen, selection)
      | None -> normal_mode (t, screen, selection)
      end
    end

  | _ -> selection_mode (t, screen, selection)
end

and command (t, screen, selection) action = begin
  match action with
  | ("w", file) -> (* Save the file *)
      Odf.save t file;
      redraw t screen selection;
      normal_mode (t, screen, selection)
  | ("enew", _) -> (* Start a new spreadsheet *)
      let selection = Selection.create (1, 1)
      and sheet = Sheet.create catalog in
      redraw sheet screen selection;
      normal_mode (sheet, screen, selection)
  | ("q", _) ->  (* Quit *)
      t
  | _ ->
      redraw t screen selection;
      normal_mode (t, screen, selection)
end

let () = begin

  let sheet =
    if Array.length Sys.argv = 1 then
      Sheet.create catalog
    else
      Odf.load catalog Sys.argv.(1)

  and selection = Selection.create (1, 1) in

  Screen.run (fun window ->
    redraw sheet window selection;
    ignore @@ normal_mode (sheet, window, selection)
  )
end