-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.pas
4419 lines (4006 loc) · 119 KB
/
editor.pas
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ $Id$
OpenXP editor unit
Copyright (C) 1991-2001 Peter Mandrella
Copyright (C) 2000-2002 OpenXP team (www.openxp.de)
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}
{$I xpdefine.inc}
{ OpenXP editor unit }
unit editor;
interface
uses
keys, eddef, Lister;
const
EdTempFile = 'TED.TMP'; //todo: filenames
EdConfigFile = 'EDITOR.CFG';
EdGlossaryFile = 'GLOSSARY.CFG';
var
EdSelcursor : boolean = false; { Auswahllistencursor }
OtherQuoteChars : boolean = false; { Andere Quotezeichen neben > }
EditResetoldpos : boolean = false;
type
charr = array[0..65500] of char;
charrp = ^charr;
EdToken = Byte;
EdTProc = function(var t:taste):boolean; { true = beenden }
var
laststartline : longint=0; { fuer Z-Anzeige }
lastscx : integer=1;
lastscy : integer=1; { Bildschirm (Cursor) }
lastxoffset : integer=0;
procedure EdInitDefaults(color:boolean); { einmal bei Programmstart }
procedure EdSetScreenwidth(w:byte); { globale Einstellungen }
function EdInit(l,r,o,u:byte; rand:integer; savesoftbreaks:boolean;
NeuerAbsatzUmbruch:byte; iOtherQuoteChars:boolean):ECB;
function EdLoadFile(ed:ECB; fn:string; sbreaks:boolean; umbruch:byte):boolean;
function EdEdit(ed:ECB):EdToken;
function EdSave(ed:ECB):boolean;
procedure EdExit(var ed:ECB); { Release }
procedure EdSetTproc(ed:ECB; tp:EdTProc); { lokale Einstellungen }
procedure EdGetProcs(var p:EdProcs);
procedure EdSetProcs(p:EdProcs);
procedure EdSetLanguage(ld:LangData);
procedure EdSetColors(col:EdColrec);
procedure EdSetForcecr(newcr:boolean);
procedure EdPointswitch(yuppieon:boolean);
procedure EdGetConfig(var cf:EdConfig);
procedure EdSetConfig(cf:EdConfig);
procedure EdSetUkonv(umlaute_konvertieren:boolean);
procedure EdAutoSave;
function EdModified(ed:ECB):boolean; { externer Zugriff }
function EdFilename(ed:ECB):string;
procedure EdAddToken(ed:ECB; t:EdToken);
function EddefQuitfunc(ed:ECB):taste;
function EddefOverwrite(ed:ECB; fn:string):taste;
procedure EddefMsgproc(txt:string; error:boolean);
procedure EddefFileproc(ed:ECB; var fn:string; save,uuenc:boolean);
function EddefFindFunc(ed:ECB; var txt:string; var igcase:boolean):boolean;
function EddefReplFunc(ed:ECB; var txt,repby:string; var igcase:boolean):boolean;
procedure Glossary_ed(LSelf: TLister; var t:taste); {Lister-Tastenabfrage fuer Glossary-Funktion }
implementation { ------------------------------------------------ }
uses
sysutils,
{$IFDEF unix}
xpcurses,
{$ENDIF}
osdepend,mouse,clip,xpconst,
typeform,fileio,inout,maus2,winxp,printerx, xp0, xp1, xp2, xpe, xp_uue,
xpglobal;
const maxgl = 60;
asize = 16; { sizeof(absatzt)-sizeof(absatzt.cont) }
maxtokens = 128;
maxabslen = 16363;
var screenwidth : byte = 80;
message : string = '';
type
// charr = array[0..65500] of char;
// charrp = ^charr;
absatzp = ^absatzt;
absatzt = packed record
next,prev : absatzp;
size,msize : smallword; { msize = allokierte Groesse }
umbruch : boolean;
fill : array[1..3] of byte;
cont : charr;
end;
position = record
absatz : absatzp;
offset : integer;
end;
edp = ^EdData;
EdData = record { je aktivem Editorobjekt }
lastakted : edp;
x,y,w,h,gl : Integer; { --- Startup }
edfile : string;
showfile : string[40];
savesoftbreak : boolean; { beim Speichern }
tproc : EdTProc;
Procs : EdProcs;
root : absatzp;
firstpar : absatzp; { --- akt.Pos.: 1. Absatz auf Schirm }
firstline : integer; { Zeile innerhalb dieses Absatzes }
startline : longint; { fuer Z-Anzeige }
scx,scy : integer; { Bildschirm (Cursor) }
xoffset : integer; { x-Anzeigeoffset }
col : EdColRec; { --- Daten/Status }
insertmode : boolean;
modified : boolean;
rrand : byte; { rechter Umbruch-Rand }
tokenfifo : array[0..maxtokens-1] of EdToken; { --- Befehle }
tnextin : byte;
tnextout : byte;
absatzende : char;
lastpos : position; { fuer Ctrl-Q-P }
{ disp: 1 = Markierung oberhalb Bildausschnitt, 2=in, 3=unterhalb }
block : array[1..7] of record { 3..7 = Marker }
pos : position;
disp : Byte;
end;
blockinverse : boolean; { Endmarkierung vor Anfangsmark. }
blockhidden : boolean; { Blockmarkierung ausgeschaltet }
na_umbruch : Boolean;
forcecr : boolean; { CR am Textende beim Speichern }
pointswitch : boolean; { XPoint-Editor }
Config : EdConfig;
ukonv : boolean;
autosave : boolean;
end;
delnodep = ^delnode;
delnode = record
absatz : absatzp;
next : delnodep;
end;
modiproc = procedure(var data; Size: Integer);
var Defaults : edp;
language : ldataptr = nil;
akted : edp;
delroot : delnodep; { Liste geloeschter Bloecke }
ClipBoard: absatzp;
NoCursorsave : boolean;
ECBOpen : integer; { Semaphor fr Anzahl der offenen ECB's }
{ SeekStr:
Sucht den String s im len Byte langen Chararray data
je nach igcase case-sensitiv oder insensitiv (wohl
in Bezug auf den DOS-Zeichensatz) und liefert die
Position der ersten Fundstelle oder -1 zurueck.
Das Fehlen einer praezisen Typdeklaration fuer data
erklaert sich daraus, dass diese Funktion ein Ersatz
fuer eine historische Assemblerfunktion ist.
Verwendet wird der Algorithmus von Knuth, Morris und
Pratt.
}
function SeekStr(var data; len: LongWord;
var s : string; igcase:boolean):integer;
const
uc : array[#0..#255] of char =
(
#0, #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, #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,#123,#124,#125,#126,#127,
#128,#154,#144,#131,#142,#133,#143,#128,
#136,#137,#138,#139,#140,#141,#142,#143,
#144,#146,#146,#147,#153,#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
);
function cc(c : char):char; { Konvertiert zu Grossbuchstaben, falls igcase }
begin
if igcase then
cc := uc[c]
else
cc := c;
end;
function kmp(t:charrp; tl:longint; const suchwort:string): longint;
var wortl : longint;
kmpnext : array of longint;
i,j : longint;
found : boolean;
begin
wortl := length(suchwort);
j := -1;
i := 0;
kmp := -1;
setlength(kmpnext,wortl+1);
kmpnext[0] := -1;
while (i < wortl) do
begin
while ((j >= 0) and (cc(suchwort[j+1]) <> cc(suchwort[i+1]))) do
j := kmpnext[j];
j := j+1;
i := i+1;
kmpnext[i] := j;
end;
i := 0;
j := 0;
found := false;
while ((not found) and (i < tl)) do
begin
while ((j >= 0) and (cc(suchwort[j+1]) <> cc(t[i]))) do
j := kmpnext[j];
j := j+1;
i := i+1;
if (j >= wortl) then
begin
found := true;
kmp := i - j;
{ j := kmpnext[j]; // sollte hier stehen, sofern man weitersucht }
end
end;
end;
begin
SeekStr := kmp(charrp(@data),len,s);
end;
// rueckwaerts von data[zlen] bis data[0] nach erster Umbruchstelle suchen
function FindUmbruch(var data; zlen : integer; safe : boolean):integer;
type
tpc = array[0..MaxInt div 2] of Char;
pc = ^tpc;
chartype = (c_alnum,c_space,c_minus,c_slash,c_other);
action = (ret0,ret1,cont);
// state = (none, alnum, slash);
const
todo : array[c_alnum..c_other,c_alnum..c_other] of action =
(
(cont,cont,cont,ret1,cont),
(ret0,ret0,ret0,ret0,ret0),
(ret0,cont,cont,cont,cont),
(cont,cont,cont,ret1,cont),
(cont,cont,cont,ret1,cont)
);
function ct(c : char):chartype;
begin
case c of
' ' : ct := c_space;
'-' : ct := c_minus;
'/' : ct := c_slash;
'0'..'9','a'..'z','A'..'Z',#128..#165 : ct := c_alnum
else
ct := c_other;
end
end; { ct }
function fu(c : pc; zlen: integer):integer;
// Cave: greift auf c[zlen+1] zu!!!
var
left,right : chartype;
begin
right := c_other;
if safe then // Der Aufrufer hat sichergestellt, dass der Zugriff auf
begin // c[zlen+1] sicher ist.
right := ct(c[zlen+1]);
if (right = c_slash) then
right := c_other;
end;
while (zlen >= 0) do
begin
left := ct(c[zlen]);
case todo[left,right] of
cont : begin right := left; zlen := zlen-1; end;
ret0 : begin fu := zlen ; break; end;
ret1 : begin
if ((zlen = 0) and (right = c_slash)) then
fu := 0
else
fu := zlen+1;
break;
end
end; { case }
end;
if (zlen < 0) then fu := 0;
end;
begin
if (zlen <= 0) then
FindUmbruch := 0
else
FindUmbruch := fu(pc(@data),zlen);
end;
procedure FlipCase(var data; size: Integer);
var
i: integer;
begin
if size>0 then
for i:=0 to size-1 do
if UpCase(TCharArray(data)[i])=TCharArray(data)[i] then
TCharArray(data)[i]:=LoCase(TCharArray(data)[i])
else
TCharArray(data)[i]:=UpCase(TCharArray(data)[i]);
end;
{ --------------------------------------------------- Einstellungen }
procedure errsound;
begin
write(#7);
end;
function AskJN(ed:ECB; nr:byte; default:char):taste;
var t,tt : taste;
txt : string[80];
begin
with edp(ed)^ do begin
case nr of
1 : txt:=language^.askquit;
2 : txt:=language^.askoverwrite;
end;
attrtxt(col.colstatus);
wrt(x,y,forms(txt,w));
t:=default;
repeat
mwrt(x+length(txt),y,t);
GotoXY(x+length(txt)-1, y);
get(tt,curon);
tt:=UpperCase(tt);
if tt=#13 then t:=default
else if tt=keyesc then t:=keyesc
else if tt>' ' then t:=tt;
until (tt=language^.ja) or (tt=language^.nein) or (tt=keyesc) or (tt=keycr);
AskJN:=t;
end;
end;
{$IFDEF FPC }
{$HINTS OFF }
{$ENDIF }
function EddefQuitfunc(ed:ECB):taste;
begin
EddefQuitfunc:=AskJN(ed,1,language^.ja);
end;
function EddefOverwrite(ed:ECB; fn:string):taste;
begin
EddefOverwrite:=AskJN(ed,2,language^.ja);
end;
function EddefFindFunc(ed:ECB; var txt:string; var igcase:boolean):boolean;
begin
errsound;
EddefFindfunc:=false;
end;
function EddefReplFunc(ed:ECB; var txt,repby:string; var igcase:boolean):boolean;
begin
errsound;
EddefReplFunc:=false;
end;
procedure EddefMsgproc(txt:string; error:boolean);
begin
message:=txt;
errsound;
end;
{$IFDEF FPC }
{$HINTS ON }
{$ENDIF }
procedure EddefFileproc(ed:ECB; var fn:string; save,uuenc:boolean);
var brk : boolean;
mf : char;
begin
with edp(ed)^ do begin
attrtxt(col.colstatus);
wrt(x,y,sp(w));
fn:='';
mf:=fchar; fchar:=' ';
bd(x,y,'Block '+iifs(save,'speichern','laden')
+iifs(uuenc,' und UU-kodieren','')
+': ',fn,min(w-20,70),1,brk);
fchar:=mf;
if brk then fn:='';
end;
end;
procedure EdInitDefaults(color:boolean);
var t : text;
s : string;
p : byte;
i : integer;
begin
new(Defaults);
akted:=Defaults;
fillchar(Defaults^,sizeof(Defaults^),0);
with Defaults^ do begin
with col do
if color then begin
coltext:=$7; colstatus:=$c; colmarked:=$17;
colendmark:=3;
for i:=1 to 9 do colquote[i]:=3;
colmenu:=$71; colmenuhi:=$74; colmenuinv:=$17; colmenuhiinv:=$17;
end
else begin
coltext:=7; colstatus:=$f; colmarked:=$70;
colmenu:=$70; colmenuhi:=$f; colmenuinv:=7; colmenuhiinv:=7;
end;
insertmode:=true;
Procs.QuitFunc:=EddefQuitfunc;
Procs.Overwrite:=EddefOverwrite;
Procs.MsgProc:=EddefMsgProc;
Procs.FileProc:=EddefFileProc;
Procs.FindFunc:=EddefFindFunc;
Procs.ReplFunc:=EddefReplFunc;
forcecr:=false;
config.absatzendezeichen:='ú';
config.rechter_rand:=74;
config.AutoIndent:=true;
config.PersistentBlocks:=true;
config.QuoteReflow:=true;
assign(t,EdConfigFile);
if existf(t) then begin
reset(t);
while not eof(t) do begin
readln(t,s);
LoString(s);
p:=cpos('=',s);
if p>0 then
if LeftStr(s,p-1)='rechterrand' then
config.rechter_rand:=ival(mid(s,p+1))
else if LeftStr(s,p-1)='absatzende' then
config.absatzendezeichen:=iifc(p<length(s),s[p+1],' ')
else if LeftStr(s,p-1)='autoindent' then
config.AutoIndent:=(mid(s,p+1)<>'n')
else if LeftStr(s,p-1)='persistentblocks' then
config.PersistentBlocks:=(mid(s,p+1)<>'n')
else if LeftStr(s,p-1)='quotereflow' then
config.QuoteReflow:=(mid(s,p+1)<>'n');
end;
close(t);
end;
end;
new(language);
with language^ do begin
zeile:='Ze'; spalte:='Sp';
ja:='J'; nein:='N';
errors[1]:='zu wenig freier Speicher';
errors[2]:='Absatz zu gross';
errors[3]:='Fehler beim Laden des Textes';
errors[4]:='Fehler beim Speichern';
errors[5]:='Fehler: Datei nicht vorhanden';
errors[6]:='Text wurde nicht gefunden.';
askquit:='Text speichern (j/n) ';
askoverwrite:='Datei existiert schon - ueberschreiben (j/n) ';
askreplace:='Text ersetzen (Ja/Nein/Alle/Esc)';
replacechr:='JNA';
ersetzt:=' Textstellen ersetzt';
drucken:='Drucken ...';
menue[0]:='Block';
menue[1]:='^Kopieren *';
menue[2]:='^Ausschneiden -';
menue[3]:='^Einfuegen +';
menue[4]:='^Laden ^KR';
menue[5]:='La^den UUE ^KU';
menue[6]:='^Speichern ^KW';
menue[7]:='-';
menue[8]:='S^uchen ^QF';
menue[9]:='E^rsetzen ^QL';
menue[10]:='Weitersuchen ^L';
menue[11]:='-';
menue[12]:='^Umbruch aus F3';
menue[13]:='U^mbruch ein F4';
menue[14]:='-';
menue[15]:='^Optionen';
menue[16]:='-';
menue[17]:='Beenden ESC';
end;
delroot:=nil;
Clipboard:=nil;
end;
procedure EdSetLanguage(ld:LangData);
begin
language^:=ld;
end;
procedure EdSetScreenwidth(w:byte);
begin
screenwidth:=w;
end;
procedure EdSetColors(col:EdColrec);
begin
akted^.col:=col;
end;
procedure EdGetProcs(var p:EdProcs);
begin
p:=akted^.Procs;
end;
procedure EdSetProcs(p:EdProcs);
begin
akted^.Procs:=p;
end;
procedure EdSetForcecr(newcr:boolean);
begin
akted^.forcecr:=newcr;
end;
procedure EdPointswitch(yuppieon:boolean);
begin
akted^.pointswitch:=yuppieon;
end;
procedure EdGetConfig(var cf:EdConfig);
begin
cf:=akted^.config;
end;
procedure EdSetConfig(cf:EdConfig);
begin
if akted <> nil then
akted^.config:=cf;
end;
procedure EdSetUkonv(umlaute_konvertieren:boolean);
begin
akted^.ukonv:=umlaute_konvertieren;
end;
procedure EdAutoSave;
begin
akted^.autosave:=true;
end;
{ ------------------------------------------------ externer Zugriff }
function EdModified(ed:ECB):boolean;
begin
EdModified:=edp(ed)^.modified;
end;
function EdFilename(ed:ECB):string;
begin
EdFilename:=edp(ed)^.edfile;
end;
procedure EdAddToken(ed:ECB; t:EdToken);
var tnext : integer;
begin
with edp(ed)^ do begin
tnext:=tnextin+1;
if tnext=maxtokens then tnext:=0;
if tnext<>tnextout then begin
tokenfifo[tnextin]:=t;
tnextin:=tnext;
end;
end;
end;
{ ------------------------------- Liste geloeschter Bloecke verwalten }
procedure AddDelEntry(ap:absatzp);
var dnp : delnodep;
begin
new(dnp);
dnp^.absatz:=ap;
dnp^.next:=delroot;
delroot:=dnp;
end;
function GetDelEntry:absatzp;
var dnp : delnodep;
begin
if delroot=nil then
GetDelEntry:=nil
else begin
GetDelEntry:=delroot^.absatz;
dnp:=delroot^.next;
dispose(delroot);
delroot:=dnp;
end;
end;
procedure freeblock(var ap:absatzp); forward;
procedure FreeDellist; { Liste geloeschter Bloecke freigeben }
var ap : absatzp;
begin
repeat
ap:=GetDelEntry;
freeblock(ap);
until delroot=nil;
end;
{ -------------------------------------------------------- Speicher }
procedure error(nr:integer);
var txt : string[80];
begin
txt:=language^.errors[nr];
akted^.Procs.MsgProc(txt,true);
end;
function AllocAbsatz(size:integer):absatzp;
var
ms : integer;
begin
ms:=(size+15) and $fff0; { auf 16 Bytes aufrunden }
Getmem(Result, asize + ms); // evtl. hier EOutOfMemory-Fehler abfangen
Fillchar(Result^, asize, 0); { next, prev implizit auf NIL setzen, Rest auf 0 }
Result^.size:=size;
Result^.msize:=ms;
Result^.umbruch:=true;
end;
function freeabsatz(const p:absatzp): absatzp;
var p2: absatzp;
begin
if assigned(p) then begin
p2:=p;
freemem(p2,asize+p^.msize);
end;
result:=nil;
end;
{ ------------------------------------------------------------ Edit }
{ Block freigeben }
procedure FreeBlock(var ap:absatzp);
var p : absatzp;
begin
while assigned(ap) do begin { Text freigeben }
p:=ap^.next;
freeabsatz(ap);
ap:=p;
end;
end;
{ sbreaks: Softbreaks aufloesen }
{ umbruch: 0 = alles ohne Umbruch laden }
{ 1 = nur lange Zeilen ohne Softbreak ohne Umbruch laden }
{ 2 = alles mit Umbruch laden }
function LoadBlock(const fn:string; sbreaks:boolean; umbruch,rrand:byte):absatzp;
var mfm : byte;
s : string;
t : text;
p : absatzp;
tail : absatzp;
sbrk : boolean;
root : absatzp;
endcr : boolean;
procedure AppP;
begin
if root=nil then begin
root:=p; tail:=p;
end
else begin
p^.prev:=tail;
tail^.next:=p;
tail:=p;
end;
end;
begin
root:=nil;
if Fileexists(fn) then
begin
IOResult;
mfm:=filemode; filemode:= fmOpenRead + fmShareDenyWrite;
assign(t,fn); reset(t);
filemode:=mfm;
p := Pointer(1);
tail:=nil;
endcr:=false;
IOExcept(Exception);
while not eof(t) and assigned(p) do
begin
read(t,s); // read until line end (not including line end)
endcr := not eof(t); // end of file is at line end => no cr at end
readln(t); // skip over line end
IOExcept(Exception);
sbrk := (Length(s)>40) and sbreaks and (s[length(s)]=' ');
if sbrk then
SetLength(s, Length(s)-1);
p:=AllocAbsatz(length(s));
if assigned(p) then begin
p^.umbruch:=(rrand>0) and
((umbruch=2) or
((umbruch=1) and ((length(s)<=rrand) or sbrk)));
if length(s)>0 then
Move(s[1],p^.cont,length(s));
AppP;
end; // if assigned(p) ...
end; // while ...
Close(t);
IOExcept(Exception);
if endcr then
begin
p:=AllocAbsatz(0);
p^.umbruch:=(umbruch<>0);
AppP;
end;
End; // if FileExists(fn)
LoadBlock:=root;
end;
function LoadUUeBlock(fn:string):absatzp;
const blen = 45;
var mfm : byte;
s : string;
t : file;
p : absatzp;
tail : absatzp;
ibuf : tbytestream;
b_read: Integer;
root : absatzp;
procedure AppP;
begin
if root=nil then begin
root:=p; tail:=p;
end
else begin
p^.prev:=tail;
tail^.next:=p;
tail:=p;
end;
end;
procedure Absatz;
begin
p:=AllocAbsatz(length(s));
if assigned(p) then begin
p^.umbruch:=true;
Move(s[1],p^.cont,length(s));
AppP;
end else
raise EOutOfMemory.Create('in loadUUEblock'); //or what?
end;
begin
root:=nil;
if Fileexists(fn) then
begin
mfm:=filemode; filemode:= fmOpenRead + fmShareDenyWrite;
assign(t,fn); reset(t,1);
filemode:=mfm;
p:=pointer(1);
tail:=nil;
fn := ExtractFileName(fn);
s:='begin 644 '+fn;
while not eof(t) and assigned(p) do begin
if s='' then begin
blockread(t,ibuf,blen,b_read);
s := encode_UU(ibuf,b_read);
end;
Absatz;
s:='';
if eof(t) then for b_read:=1 to 3 do begin
if b_read=1 then s:='`'
else if b_read=2 then s:='end'
else if b_read=3 then str(filesize(t),s);
Absatz;
end;
end;
close(t);
if ioresult<>0 then error(3);
end;
LoadUUeBlock:=root;
end;
function EdLoadFile(ed:ECB; fn:string; sbreaks:boolean; umbruch:byte):boolean;
begin
with edp(ed)^ do
begin
edfile:=ExpandFilename(fn);
showfile:=' '+fitpath(edfile,max(14,w-40));
if assigned(root) then FreeBlock(root);
EdLoadFile:=false;
root:=LoadBlock(fn,sbreaks,umbruch,rrand);
if root=nil then
root:=AllocAbsatz(0);
firstpar:=root; firstline:=1; { Anzeigeposition setzen }
scx:=1; scy:=1;
block[1].pos.absatz:=nil;
block[1].disp := 3; { Anfangsmarkierung am Ende }
block[2].pos.absatz:=root;
block[2].disp := 1; { Endmarkierung am Anfang }
blockinverse:=true;
end;
end;
{ NeuerAbsatzUmbruch: 0=nein, 1=Kopie, 2=ja }
function EdInit(l,r,o,u:byte; rand:integer; savesoftbreaks:boolean;
NeuerAbsatzUmbruch:byte; iOtherQuoteChars:boolean):ECB;
var ed : edp;
begin
new(ed);
Move(Defaults^,ed^,sizeof(Defaults^));
ed^.lastakted:=akted;
akted:=ed;
with ed^ do begin
x:=l; w:=r-l+1;
y:=o; h:=min(u-o+1,maxgl+1);
gl:=h-1;
if rand<>0 then rrand:=rand
else rrand:=Config.rechter_rand;
absatzende:=Config.absatzendezeichen;
savesoftbreak:=savesoftbreaks;
na_Umbruch:=(NeuerAbsatzUmbruch = 2);
OtherQuoteChars:=iOtherQuoteChars;
end;
inc(ecbopen);
EdInit:=ed;
end;
procedure EdSetTproc(ed:ECB; tp:EdTProc);
begin
edp(ed)^.tproc:=tp;
end;
{ Positionszeiger in Absatz auf naechsten Zeilenbeginn bewegen }
{ Offset muss auf Zeilenanfang zeigen! }
function Advance(ap:absatzp; offset,rand:word):integer;
var zlen : integer; { Zeilenlaenge }
safe : boolean; { ist es sicher, dass FindUmbruch nicht auf unallozierten Speicher trifft? }
begin
with ap^ do
if not umbruch or (size-offset<=rand) then
Advance:=size
else
begin
// Wird nur erreicht, wenn size-offset > rand gilt, dann aber
// gilt size-offset-1 >= rand und damit
// min(rand,size-offset-1) = rand. (1)
// Gefaehrlich wird FindUmbruch nur, wenn rand = size-offset-1.
safe := not (rand = size-offset-1);
// Wegen (1) kann
// zlen:=min(rand,size-offset-1);
// durch
zlen := rand;
// ersetzt werden.
// Folglich ist auch der Test auf zlen = rand in
// if (zlen=rand) and (cont[offset+zlen] in ['-','/']) then dec(zlen);
// immer true, und man kann verkuerzen zu
if (cont[offset+zlen] in ['-','/']) then
begin
dec(zlen);
safe := true; // Nach Dekrement ist cont[offset+zlen+1] wohldefiniert.
end;
zlen:=FindUmbruch(cont[offset],zlen,safe); { in EDITOR.ASM }
if zlen=0 then
Advance:=offset+rand
else
Advance:=offset+zlen+1;
end;
end;
{ Block von pstart bis pende in Datei schreiben }
function SaveBlock(pstart,pende:position; fn:string; rand:integer;
softbreak,overwrite,forcecr:boolean):boolean;
const crlf : string[2] = #13#10;
spc : string[3] = ' '#13#10;
var ap : pointer;
f : file;
ofs : integer;
nxo : integer;
ofs0,ofse : integer;
cr : boolean;
begin
if overwrite then MakeBak(fn, ExtBak);
assign(f,fn);
if not overwrite then begin
reset(f,1); seek(f,filesize(f)); end;
if overwrite or (ioresult<>0) then
rewrite(f,1);
ap:=pstart.absatz;
ofs0:=pstart.offset;
ofse:=maxint;
cr:=true;
while assigned(ap) do begin
if ap=pende.absatz then ofse:=pende.offset;
with absatzp(ap)^ do
if softbreak then
begin
ofs:=0;
{ Signaturtrenner beachten }
if (size<>3) or (cont[0]<>'-') or (cont[1]<>'-') or (cont[2]<>' ') then
{ Signaturtrenner, nicht anfassen }
while (size>0) and (cont[size-1]=' ') do
dec(size);
while (ofs<min(size,ofse)) do
begin
nxo:=Advance(ap,ofs,rand);
blockwrite(f,cont[ofs],min(nxo,ofse)-ofs);
if nxo<min(size,ofse) then
begin
blockwrite(f,spc[1],3); cr:=true;
end else
cr:=false;
ofs:=nxo;
end;
end else
begin
blockwrite(f,cont[ofs0],min(size,ofse)-ofs0);