gnoga_2.1.2_5f127c56/components/ace_editor/src/gnoga-gui-plugin-ace_editor-console_io.adb

  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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
------------------------------------------------------------------------------
--                                                                          --
--                   GNOGA - The GNU Omnificent GUI for Ada                 --
--                                                                          --
--           G N O G A . G U I . P L U G I N . A C E _ E D I T O R .        --
--                            C O N S O L E _ I O                           --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                                                                          --
--                     Copyright (C) 2017 Pascal Pignard                    --
--                                                                          --
--  This library is free software;  you can redistribute it and/or modify   --
--  it under terms of the  GNU General Public License  as published by the  --
--  Free Software  Foundation;  either version 3,  or (at your  option) any --
--  later version. This library 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.                    --
--                                                                          --
--  As a special exception under Section 7 of GPL version 3, you are        --
--  granted additional permissions described in the GCC Runtime Library     --
--  Exception, version 3.1, as published by the Free Software Foundation.   --
--                                                                          --
--  You should have received a copy of the GNU General Public License and   --
--  a copy of the GCC Runtime Library Exception along with this program;    --
--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see   --
--  <http://www.gnu.org/licenses/>.                                         --
--                                                                          --
--  As a special exception, if other files instantiate generics from this   --
--  unit, or you link this unit with other files to produce an executable,  --
--  this  unit  does not  by itself cause  the resulting executable to be   --
--  covered by the GNU General Public License. This exception does not      --
--  however invalidate any other reasons why the executable file might be   --
--  covered by the  GNU Public License.                                     --
--                                                                          --
--  For more information please go to http://www.gnoga.com                  --
------------------------------------------------------------------------------

with Ada.Characters.Wide_Wide_Latin_1;
with Ada.Strings;

package body Gnoga.Gui.Plugin.Ace_Editor.Console_IO is

   protected body Text_Buffer is
      procedure Write (Line : in String) is
      begin
         Append (Buffer, Line);
         Append (Buffer, Ada.Characters.Wide_Wide_Latin_1.CR);
         NL := True;
      end Write;

      entry Read
        (Line : out String;
         Last : out Natural) when NL
      is
         Ind : constant Natural := Index (Buffer, From_Unicode (Ada.Characters.Wide_Wide_Latin_1.CR)) - 1;
      begin
         Last := Line.First + Ind - 1;
         Line := Slice (Buffer, 1, Ind);
         Delete (Buffer, 1, Ind + 1); --  Supress CR in addition
         NL := Index (Buffer, From_Unicode (Ada.Characters.Wide_Wide_Latin_1.CR)) > 0;
      end Read;

      entry Read (Line : out String) when NL is
         Ind : constant Natural := Index (Buffer, From_Unicode (Ada.Characters.Wide_Wide_Latin_1.CR)) - 1;
      begin
         Line := Slice (Buffer, 1, Ind);
         Delete (Buffer, 1, Ind + 1); --  Supress CR in addition
         NL := Index (Buffer, From_Unicode (Ada.Characters.Wide_Wide_Latin_1.CR)) > 0;
      end Read;

      entry Read (Ch : out Unicode_Character) when Length (Buffer) > 0 is
      begin
         Ch := Element (Buffer, 1);
         Delete (Buffer, 1, 1);
         NL := Index (Buffer, From_Unicode (Ada.Characters.Wide_Wide_Latin_1.CR)) > 0;
      end Read;

      procedure Get
        (Ch        : out Unicode_Character;
         Available : out Boolean)
      is
      begin
         Available := Length (Buffer) > 0;
         if Available then
            Ch := Element (Buffer, 1);
            Delete (Buffer, 1, 1);
         end if;
         NL := Index (Buffer, From_Unicode (Ada.Characters.Wide_Wide_Latin_1.CR)) > 0;
      end Get;

      procedure Look
        (Ch        : out Unicode_Character;
         Available : out Boolean)
      is
      begin
         Available := Length (Buffer) > 0;
         if Available then
            Ch := Element (Buffer, 1);
         end if;
      end Look;
   end Text_Buffer;

   ------------
   -- Create --
   ------------

   overriding procedure Create
     (Console : in out Console_IO_Type;
      Parent  : in out Gnoga.Gui.Base.Base_Type'Class;
      ID      : in     String := "")
   is
   begin
      Ace_Editor_Type (Console).Create (Parent, ID);
      Console.Show_Gutter (False);
      Console.Set_Highlight_Active_Line (False);
      Console.Set_Show_Print_Margin (False);
      Console.Anchor.Create (Console, 0, 0);
      Console.Editor_Execute ("gnoga_prompt=0;");
      Console.Editor_Execute
        ("sendEvent = function (e, m) {ws.send ('" & Image (Console.Unique_ID) & "|' + e + '|' + m);}");
      Console.Editor_Execute
        ("commands.on('exec', function(e) {" & "    if (e.command.readOnly) return;" & "    var editableRow = " &
         Console.Editor_Var & ".session.getLength() - 1;" &
         "    var deletesLeft = e.command.name == 'backspace' || e.command.name == 'removewordleft';" &
         "    var notEditable = " & Console.Editor_Var & ".selection.getAllRanges().some(function(r) {" &
         "        if (deletesLeft && r.start.column <= " & Console.Editor_Var & ".gnoga_prompt && r.end.column <= " &
         Console.Editor_Var & ".gnoga_prompt) return true;" & "        if (deletesLeft && (r.start.column < " &
         Console.Editor_Var & ".gnoga_prompt || r.end.column < " & Console.Editor_Var & ".gnoga_prompt)) return true;" &
         "        if (r.start.column < " & Console.Editor_Var & ".gnoga_prompt && r.end.column < " &
         Console.Editor_Var & ".gnoga_prompt) return true;" &
         "        return r.start.row != editableRow || r.end.row != editableRow; " & "    });" &
         "    if (notEditable)" & "        e.preventDefault();" & "});");
      Console.Editor_Execute
        ("commands.bindKeys({" & "    'Shift-Return|Ctrl-Return|Alt-Return': function(cmdLine) {}," &
         "    'Esc|Shift-Esc': function(cmdLine){" & "        cmdLine.session.remove(" &
         "          {start:{row:cmdLine.session.getLength()-1," & "                  column: cmdLine.gnoga_prompt}," &
         "           end: {row:cmdLine.session.getLength()-1," &
         "                 column:cmdLine.session.getDocumentLastRowColumn(cmdLine.session.getLength()-1,0)}});}," &
         "    'Return': function(cmdLine){" & "        cmdLine.sendEvent ('get_line'," &
         "          cmdLine.session.getTextRange(" & "            {start:{row:cmdLine.session.getLength()-1," &
         "                    column: cmdLine.gnoga_prompt}," &
         "             end: {row:cmdLine.session.getLength()-1," &
         "                   column:cmdLine.session.getDocumentLastRowColumn(cmdLine.session.getLength()-1,0)}}));" &
         "        cmdLine.navigateFileEnd();" & "        cmdLine.insert('\n');}" & "});");
   end Create;

   ---------------------
   -- Set_Line_Length --
   ---------------------

   procedure Set_Line_Length
     (Console : in out Console_IO_Type;
      To      :        Count)
   is
   begin
      Console.Set_Use_Wrap_Mode (To /= Unbounded);
      Console.Wrap_Limit (To);
   end Set_Line_Length;

   ---------------------
   -- Set_Page_Length --
   ---------------------

   procedure Set_Page_Length
     (Console : in Console_IO_Type;
      To      :    Count)
   is
   begin
      --  Generated stub: replace with real body!
      pragma Compile_Time_Warning (Standard.True, "Set_Page_Length unimplemented");
      raise Program_Error with "Unimplemented procedure Set_Page_Length";
   end Set_Page_Length;

   -----------------
   -- Line_Length --
   -----------------

   function Line_Length
     (Console : in Console_IO_Type)
      return Count
   is
   begin
      return Console.Wrap_Limit;
   end Line_Length;

   -----------------
   -- Page_Length --
   -----------------

   function Page_Length
     (Console : in Console_IO_Type)
      return Count
   is
   begin
      return Console.Last_Visible_Row - Console.First_Visible_Row;
   end Page_Length;

   --------------
   -- New_Line --
   --------------

   overriding procedure New_Line (Console : in out Console_IO_Type) is
   begin
      Console.New_Line (1);
   end New_Line;

   procedure New_Line
     (Console : in out Console_IO_Type;
      Spacing :        Positive_Count)
   is
   begin
      for I in 1 .. Spacing loop
         Console.Anchor.Insert_New_Line_At_Anchor;
      end loop;
      Console.Editor_Execute ("gnoga_prompt=" & Image (Console.Anchor.Position.Column) & ';');
   end New_Line;

   ---------------
   -- Skip_Line --
   ---------------

   procedure Skip_Line
     (Console : in out Console_IO_Type;
      Spacing :        Positive_Count := 1)
   is
      Ch : Unicode_Character;
   begin
      for Line in 1 .. Spacing loop
         loop
            Console.Text.Read (Ch);
            exit when Ch = Ada.Characters.Wide_Wide_Latin_1.CR;
         end loop;
      end loop;
   end Skip_Line;

   -----------------
   -- End_Of_Line --
   -----------------

   function End_Of_Line
     (Console : in out Console_IO_Type)
      return Boolean
   is
      Item : Unicode_Character;
      EOL  : Boolean;
   begin
      Console.Look_Ahead (Item, EOL);
      return EOL;
   end End_Of_Line;

   --------------
   -- New_Page --
   --------------

   procedure New_Page (Console : in out Console_IO_Type) is
   begin
      Console.New_Line (Console.Page_Length);
   end New_Page;

   ---------------
   -- Skip_Page --
   ---------------

   procedure Skip_Page (Console : in Console_IO_Type) is
   begin
      --  Generated stub: replace with real body!
      pragma Compile_Time_Warning (Standard.True, "Skip_Page unimplemented");
      raise Program_Error with "Unimplemented procedure Skip_Page";
   end Skip_Page;

   -----------------
   -- End_Of_Page --
   -----------------

   function End_Of_Page
     (Console : in Console_IO_Type)
      return Boolean
   is
   begin
      --  Generated stub: replace with real body!
      pragma Compile_Time_Warning (Standard.True, "End_Of_Page unimplemented");
      raise Program_Error with "Unimplemented function End_Of_Page";
      return End_Of_Page (Console => Console);
   end End_Of_Page;

   -------------
   -- Set_Col --
   -------------

   procedure Set_Col
     (Console : in out Console_IO_Type;
      To      :        Positive_Count)
   is
      Pos : constant Position_Type := Console.Anchor.Position;
   begin
      Console.Anchor.Position ((Pos.Row, To));
   end Set_Col;

   --------------
   -- Set_Line --
   --------------

   procedure Set_Line
     (Console : in out Console_IO_Type;
      To      :        Positive_Count)
   is
      Pos : constant Position_Type := Console.Anchor.Position;
   begin
      Console.Anchor.Position ((To, Pos.Column));
   end Set_Line;

   ---------
   -- Col --
   ---------

   function Col
     (Console : in Console_IO_Type)
      return Positive_Count
   is
   begin
      return Console.Anchor.Position.Column;
   end Col;

   ----------
   -- Line --
   ----------

   function Line
     (Console : in Console_IO_Type)
      return Positive_Count
   is
   begin
      return Console.Anchor.Position.Row;
   end Line;

   ----------
   -- Page --
   ----------

   function Page
     (Console : in Console_IO_Type)
      return Positive_Count
   is
   begin
      return Console.Line / Console.Page_Length;
   end Page;

   ---------
   -- Get --
   ---------

   procedure Get
     (Console : in out Console_IO_Type;
      Item    :    out Unicode_Character)
   is
   begin
      Console.Text.Read (Item);
   end Get;

   ---------
   -- Put --
   ---------

   procedure Put
     (Console : in out Console_IO_Type;
      Item    :        Unicode_Character)
   is
   begin
      Console.Anchor.Insert_Text_At_Anchor (From_Unicode (Item));
      Console.Editor_Execute ("gnoga_prompt=" & Image (Console.Anchor.Position.Column) & ';');
   end Put;

   ----------------
   -- Look_Ahead --
   ----------------

   procedure Look_Ahead
     (Console     : in out Console_IO_Type;
      Item        :    out Unicode_Character;
      End_Of_Line :    out Boolean)
   is
      Available : Boolean;
   begin
      Console.Text.Look (Item, Available);
      End_Of_Line := Available and Item = Ada.Characters.Wide_Wide_Latin_1.CR;
   end Look_Ahead;

   -------------------
   -- Get_Immediate --
   -------------------

   procedure Get_Immediate
     (Console : in out Console_IO_Type;
      Item    :    out Unicode_Character)
   is
      Available : Boolean;
   begin
      Console.Text.Get (Item, Available);
      if not Available then
         raise End_Error;
      end if;
   end Get_Immediate;

   -------------------
   -- Get_Immediate --
   -------------------

   procedure Get_Immediate
     (Console   : in out Console_IO_Type;
      Item      :    out Unicode_Character;
      Available :    out Boolean)
   is
   begin
      Console.Text.Get (Item, Available);
   end Get_Immediate;

   ---------
   -- Get --
   ---------

   procedure Get
     (Console : in out Console_IO_Type;
      Item    :    out String)
   is
   begin
      Console.Text.Read (Item);
   end Get;

   ---------
   -- Put --
   ---------

   overriding procedure Put
     (Console : in out Console_IO_Type;
      Message : in     String;
      Class   : in     String := "";
      ID      : in     String := "")
   is
      pragma Unreferenced (Class, ID);
   begin
      Console.Anchor.Insert_Text_At_Anchor (Message);
      Console.Editor_Execute ("gnoga_prompt=" & Image (Console.Anchor.Position.Column) & ';');
   end Put;

   --------------
   -- Get_Line --
   --------------

   procedure Get_Line
     (Console : in out Console_IO_Type;
      Item    :    out String;
      Last    :    out Natural)
   is
   begin
      Console.Text.Read (Item, Last);
   end Get_Line;

   --------------
   -- Get_Line --
   --------------

   function Get_Line
     (Console : in out Console_IO_Type)
      return String
   is
      Line : String;
   begin
      Console.Text.Read (Line);
      return Line;
   end Get_Line;

   --------------
   -- Put_Line --
   --------------

   overriding procedure Put_Line
     (Console : in out Console_IO_Type;
      Message : in     String;
      Class   : in     String := "";
      ID      : in     String := "")
   is
      pragma Unreferenced (Class, ID);
   begin
      Console.Anchor.Insert_Text_At_Anchor (Message);
      Console.Anchor.Insert_New_Line_At_Anchor;
      Console.Editor_Execute ("gnoga_prompt=" & Image (Console.Anchor.Position.Column) & ';');
   end Put_Line;

   ----------------------
   -- Fire_On_Get_Line --
   ----------------------

   procedure Fire_On_Get_Line
     (Console : in out Console_IO_Type;
      Line    : in     String)
   is
   begin
      Console.Text.Write (Line);
   end Fire_On_Get_Line;

   ----------------
   -- On_Message --
   ----------------

   overriding procedure On_Message
     (Object  : in out Console_IO_Type;
      Event   : in     String;
      Message : in     String)
   is
   begin
      -- Get_Line Event --
      if Event = "get_line" then
         Object.Fire_On_Get_Line (Message);
      else
         Gnoga.Gui.Base.Base_Type (Object).On_Message (Event, Message);
      end if;
   end On_Message;

end Gnoga.Gui.Plugin.Ace_Editor.Console_IO;