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
|
module filo
implicit none
type :: erow
integer::idx
character(len=1), dimension(:), allocatable::chars
contains
procedure :: size => row_size
procedure :: string_pointer => row_string_pointer
end type
type :: editorConfig
integer::cx, cy ! Cursor position in characters
integer::rowoff ! Offset of row displayed
integer::coloff ! Offset of column displayed
integer::screenrows
integer::screencols
integer::numrows
type(erow), dimension(:), allocatable::row
integer::dirty
character(len=:), pointer::filename
end type
interface editorInsertRow
module procedure editorInsertRowString
module procedure editorInsertRowArray
module procedure editorInsertRowEmpty
end interface
contains
pure function row_size(er)
implicit none
integer::row_size
class(erow), intent(in)::er
if(allocated(er%chars)) then
row_size = size(er%chars)
else
row_size = 0
end if
end function row_size
function row_string_pointer(er) result(str)
implicit none
class(erow), intent(in)::er
character(len=:), pointer::str
integer::i
if(row_size(er) == 0) then
str => null()
else
allocate(character(len=row_size(er)) :: str)
do i = 1, row_size(er)
str(i:i) = er%chars(i)
end do
end if
end function row_string_pointer
subroutine editorInsertRowEmpty(E, at)
implicit none
class(editorConfig), intent(inout)::E
integer, intent(in)::at
type(erow), dimension(:), allocatable::ertmp
if(at > E%numrows + 1) then
return
end if
allocate(ertmp(E%numrows+1))
if(allocated(E%row) .and. E%numrows > 0) then
ertmp(1:E%numrows) = E%row
end if
call move_alloc(ertmp, E%row)
if(at /= E%numrows+1) then
E%row(at+1:E%numrows+1) = E%row(at:E%numrows)
E%row(at+1:E%numrows+1)%idx = E%row(at+1:E%numrows+1)%idx+1
end if
E%row(at)%idx = at
E%numrows = E%numrows + 1
E%dirty = E%dirty + 1
end subroutine editorInsertRowEmpty
subroutine editorInsertRowArray(E, at, arr)
implicit none
class(editorConfig), intent(inout)::E
integer, intent(in)::at
character, dimension(:)::arr
if(at > E%numrows + 1) then
return
end if
call editorInsertRowEmpty(E, at)
allocate(E%row(at)%chars(size(arr)))
E%row(at)%chars = arr
end subroutine editorInsertRowArray
subroutine editorInsertRowString(E, at, s)
implicit none
class(editorConfig), intent(inout)::E
integer, intent(in)::at
character(*)::s
character, dimension(:), allocatable::ctmp
integer::i
if(at > E%numrows+1) then
return
end if
if(len(s) == 0) then
call editorInsertRowEmpty(E, at)
else
allocate(ctmp(len(s)))
do i=1, len(s)
ctmp(i) = s(i:i)
end do
call editorInsertRowArray(E, at, ctmp)
deallocate(ctmp)
end if
end subroutine editorInsertRowString
subroutine editorFreeRow(er)
implicit none
type(erow), intent(inout)::er
deallocate(er%chars)
end subroutine editorFreeRow
subroutine editorDelRow(E, at)
implicit none
class(editorConfig), intent(inout)::E
integer, intent(in)::at
if(at > E%numrows) then
return
end if
call editorFreeRow(E%row(at))
if(at < E%numrows) then
E%row(at:E%numrows-1) = E%row(at+1:E%numrows)
E%row(at:E%numrows-1)%idx = E%row(at:E%numrows-1)%idx-1
end if
E%numrows = E%numrows - 1
E%dirty = E%dirty + 1
end subroutine editorDelRow
function editorRowsToString(E) result(res)
implicit none
class(editorConfig), intent(in)::E
character(len=:), pointer::res
integer::totlen
integer::j, k, i
totlen = 0
do j=1,E%numrows
totlen = totlen + E%row(j)%size()
end do
totlen = totlen + E%numrows*len_trim(new_line(' '))
allocate(character(len=totlen) :: res)
i = 1
do j=1, E%numrows
do k=1,E%row(j)%size()
res(i:i) = E%row(j)%chars(k)
i = i + 1
end do
res(i:i+len_trim(new_line(' '))) = new_line(' ')
i = i + len_trim(new_line(' '))
end do
end function editorRowsToString
subroutine editorRowInsertChar(E, er, at, c)
implicit none
class(editorConfig), intent(inout)::E
type(erow), intent(inout)::er
integer, intent(in)::at
character, intent(in)::c
integer::padlen
character, dimension(:), allocatable::ctmp
if(at > er%size()) then
padlen = at - er%size()
allocate(ctmp(er%size() + padlen + 1))
ctmp(1:er%size()) = er%chars
if(padlen > 0) then
ctmp(er%size()+1:er%size()+padlen) = ' '
end if
else
allocate(ctmp(er%size() + 1))
ctmp(1:at-1) = er%chars(1:at-1)
ctmp(at+1:er%size() + 1) = er%chars(at:er%size())
end if
ctmp(at) = c
call move_alloc(ctmp, er%chars)
E%dirty = E%dirty + 1
end subroutine editorRowInsertChar
subroutine editorRowAppendArray(E, er, arr)
implicit none
class(editorConfig), intent(inout)::E
type(erow), intent(inout)::er
character, dimension(:), intent(in)::arr
character, dimension(:), allocatable::ctmp
allocate(ctmp(er%size()+size(arr)))
ctmp(1:er%size()) = er%chars
ctmp(er%size()+1:er%size()+size(arr)) = arr
call move_alloc(ctmp, er%chars)
E%dirty = E%dirty + 1
end subroutine editorRowAppendArray
subroutine editorRowAppendString(E, er, s)
implicit none
class(editorConfig), intent(inout)::E
type(erow), intent(inout)::er
character(*), intent(in)::s
character, dimension(:), allocatable::ctmp
integer::i
allocate(ctmp(len(s)))
do i=1,len(s)
ctmp(i) = s(i:i)
end do
call editorRowAppendArray(E, er, ctmp)
deallocate(ctmp)
end subroutine editorRowAppendString
subroutine editorRowDelChar(er, at)
implicit none
type(erow), intent(inout)::er
integer, intent(in)::at
character, dimension(:), allocatable::ctmp
if(er%size() < at) then
return
end if
if(er%size() == 1) then
deallocate(er%chars)
else
allocate(ctmp(er%size() - 1))
ctmp(1:at-1) = er%chars(1:at-1)
ctmp(at:er%size()-1) = er%chars(at+1:er%size())
call move_alloc(ctmp, er%chars)
end if
end subroutine editorRowDelChar
subroutine editorInsertChar(E, c)
implicit none
class(editorConfig), intent(inout)::E
character, intent(in)::c
integer::filerow, filecol
filerow = E%rowoff+E%cy
filecol = E%coloff+E%cx
!Print *, E%cy, filerow, E%rowoff, ':', E%cx, filecol
if(filerow >= E%numrows) then
do while(E%numrows < filerow)
call editorInsertRow(E, E%numrows, "")
end do
end if
call editorRowInsertChar(E, E%row(filerow), filecol, c)
if(E%cx == E%screencols - 1) then
E%coloff = E%coloff + 1
else
E%cx = E%cx + 1
end if
E%dirty = E%dirty + 1
end subroutine editorInsertChar
subroutine editorInsertNewline_fixcursor(E)
implicit none
class(editorConfig), intent(inout)::E
if(E%cy == E%screenrows) then
E%rowoff = E%rowoff + 1
else
E%cy = E%cy + 1
end if
E%cx = 0
E%coloff = 0
end subroutine editorInsertNewline_fixcursor
subroutine editorInsertNewline(E)
implicit none
class(editorConfig), intent(inout)::E
type(erow)::er
integer::filerow, filecol
character, dimension(:), allocatable::ctmp
filerow = E%rowoff+E%cy
filecol = E%coloff+E%cx
if(filerow > E%numrows) then
call editorInsertRow(E, filerow, "")
call editorInsertNewline_fixcursor(E)
return
else
er = E%row(filerow)
end if
filecol = min(er%size(), filecol)
if(filecol == 0) then
call editorInsertRow(E, filerow, "")
else
call editorInsertRow(E, filerow+1, er%chars(filecol:er%size()))
allocate(ctmp(filerow))
ctmp = er%chars(1:filerow)
call move_alloc(ctmp, er%chars)
end if
call editorInsertNewline_fixcursor(E)
end subroutine editorInsertNewline
subroutine editorDelChar(E)
implicit none
class(editorConfig), intent(inout)::E
integer::filerow, filecol
integer::deltax
filerow = E%rowoff+E%cy
filecol = E%coloff+E%cx
if(filerow > E%numrows) then
return
end if
if(filecol == 1) then
filecol = E%row(filerow - 1)%size()
if(E%row(filerow)%size() > 0) then
call editorRowAppendArray(e, E%row(filerow - 1), E%row(filerow)%chars)
end if
call editorDelRow(E, filerow)
if(E%cy == 1) then
E%rowoff = E%rowoff - 1
else
E%cy = E%cy - 1
end if
E%cx = filecol+1
if(E%cx > E%screencols) then
deltax = E%screencols - E%cx + 1
E%cx = E%cx - deltax
E%coloff = E%coloff + deltax
Print *, "new offset: ", E%coloff, "->", E%screencols, E%cx
end if
else
call editorRowDelChar(E%row(filerow), filecol - 1)
if(E%cx == 0 .and. E%coloff > 0) then
E%coloff = E%coloff - 1
else
E%cx = E%cx - 1
end if
end if
E%dirty = E%dirty + 1
end subroutine editorDelChar
function editorOpen(E, filename) result(success)
implicit none
class(editorConfig), intent(inout)::E
character(*), intent(in)::filename
integer::unum, ios
logical::success
character(1024)::line
E%dirty = 0
if(associated(E%filename)) then
deallocate(E%filename)
end if
allocate(character(len=len(filename))::E%filename)
E%filename = filename
open(newunit=unum, file=filename, action="read", iostat=ios, status="old")
if(ios /= 0) then
success = .false.
return
end if
read(unum, '(A)', iostat=ios) line
do while(ios == 0)
call editorInsertRow(E, E%numrows+1, trim(line))
line = ' '
read(unum, '(A)', iostat=ios) line
end do
close(unum)
E%dirty = 0
E%cx = 1
E%cy = 1
success = .true.
end function editorOpen
function editorSave(E) result(success)
implicit none
class(editorConfig), intent(inout)::E
logical::success
character(len=:), pointer::buf
integer::unum, ios
buf => editorRowsToString(E)
if(.not. associated(buf)) then
success = .false.
return
end if
open(newunit=unum, file=E%filename, status='unknown', action='write', iostat=ios)
if(ios /= 0) then
success = .false.
return
end if
write(unum, *, iostat=ios) buf
close(unum)
success = .true.
E%dirty = 0
end function editorSave
subroutine initEditor(E)
implicit none
class(editorConfig), intent(out)::E
E%cx = 1
E%cy = 1
E%rowoff = 0
E%coloff = 0
E%numrows = 0
E%dirty = 0
end subroutine initEditor
end module filo
|