File size: 6,468 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

#help_index "DolDoc/Tree"

public Bool DocTreeFind(CDoc *haystack_doc,U8 *needle_path,
  CDocEntry **_tree_entry=NULL,
  CDocEntry **_start_indent=NULL, CDocEntry **_end_indent=NULL)
{//Find tree widget start and end.
  I64 i=0,k=0;
  U8 *st1=StrNew(needle_path),*st2=MAlloc(StrLen(needle_path)+1);
  Bool res=FALSE,unlock_doc=DocLock(haystack_doc);
  CDocEntry *doc_e=haystack_doc->head.next;
  if (_tree_entry) *_tree_entry=haystack_doc;
  if (_start_indent) *_start_indent=haystack_doc;
  if (_end_indent) *_end_indent=haystack_doc;
  while (*st1 && doc_e!=haystack_doc) {
    StrFirstRem(st1,"/",st2);
    if (*st2) {
      while (doc_e!=haystack_doc) {
        if (doc_e->type_u8==DOCT_INDENT)
          i+=doc_e->attr;
        else if (i==k && doc_e->de_flags&DOCEF_TREE &&
              !StrCmp(doc_e->tag+3,st2)) {
          if (*st1)
            break;
          else {
            if (_tree_entry) *_tree_entry=doc_e;
            i=0;
            while (doc_e!=haystack_doc && doc_e->type_u8!=DOCT_INDENT)
              doc_e=doc_e->next;
            if (doc_e!=haystack_doc) {
              i=doc_e->attr;
              if (_start_indent) *_start_indent=doc_e;
              doc_e=doc_e->next;
              while (doc_e!=haystack_doc && i>0) {
                if (doc_e->type_u8==DOCT_INDENT) {
                  i+=doc_e->attr;
                  if (i<=0) {
                    if (_end_indent) *_end_indent=doc_e;
                    res=TRUE;
                    break;
                  }
                }
                doc_e=doc_e->next;
              }
            }
            goto ft_done;
          }
        }
        doc_e=doc_e->next;
      }
      k+=2;
    }
  }
ft_done:
  if (unlock_doc)
    DocUnlock(haystack_doc);
  Free(st1);
  Free(st2);
  return res;
}

public Bool DocTreeFFind(U8 *name,U8 *path)
{//Find tree widget in file.
  CDoc *doc=DocRead(name);
  Bool res=DocTreeFind(doc,path);
  DocDel(doc);
  return res;
}

public Bool DocTreeMake(CDoc *doc,U8 *path)
{//Make tree widget.
  I64 i=0,j=I64_MIN,k=0;
  U8 *st1=StrNew(path),
        *st2=MAlloc(StrLen(path)+1),
        *st3=StrNew(path);
  Bool res=TRUE,unlock_doc=DocLock(doc);
  CDocEntry *doc_e=doc->head.next;
  doc->cur_entry=doc;
  doc->cur_col=0;
  while (*st1 && doc_e!=doc) {
    StrFirstRem(st1,"/",st2);
    if (*st2) {
      while (doc_e!=doc) {
        if (doc_e->type_u8==DOCT_INDENT) {
          i+=doc_e->attr;
          if (i==j) {
            doc->cur_entry=doc_e;
            doc->cur_col=0;
            goto mt_done;
          }
        } else if (i==k && doc_e->de_flags&DOCEF_TREE &&
              !StrCmp(doc_e->tag+3,st2)) {
          Free(st3);
          st3=StrNew(st1);
          j=i;
          if (!*st1)
            res=FALSE;
          else
            break;
        }
        doc_e=doc_e->next;
      }
      k+=2;
    }
  }
mt_done:
  if (res) {
    while (*st3) {
      StrFirstRem(st3,"/",st2);
      if (*st2) {
        DocPrint(doc,"$TR+C,\"%s\"$\n$ID,2$",st2);
        doc->cur_entry=DocPrint(doc,"$ID,-2$");
        doc->cur_col=0;
      }
    }
  }
  if (unlock_doc)
    DocUnlock(doc);
  Free(st1);
  Free(st2);
  Free(st3);
  return res;
}

Bool DocTreeWriteJoin(CDoc *doc,U8 *path,Bool write,U8 *fmt,I64 argc,I64 *argv)
{//Rewrite doc tree branch.
  CDocEntry *tree_branch,*start_indent,*end_indent;
  U8 *buf=StrPrintJoin(NULL,fmt,argc,argv);
  Bool res,unlock_doc=DocLock(doc);
  if (res=DocTreeFind(doc,path,
        &tree_branch,&start_indent,&end_indent)) {
    DocCut(doc,start_indent->next,end_indent->last);
    doc->cur_entry=start_indent->next;
    doc->cur_col=doc->cur_entry->min_col;
  } else
    DocTreeMake(doc,path);
  DocPrint(doc,"%s",buf);
  if (write && DrvIsWritable(*doc->filename.name))
    DocWrite(doc);
  if (unlock_doc)
    DocUnlock(doc);
  Free(buf);
  return res;
}

Bool DocTreeAppendJoin(CDoc *doc,U8 *path,Bool write,U8 *fmt,I64 argc,I64 *argv)
{//Append to doc tree branch.
  CDocEntry *tree_branch,*start_indent,*end_indent;
  U8 *buf=StrPrintJoin(NULL,fmt,argc,argv);
  Bool res,unlock_doc=DocLock(doc);
  if (res=DocTreeFind(doc,path,
        &tree_branch,&start_indent,&end_indent)) {
    doc->cur_entry=end_indent;
    doc->cur_col=doc->cur_entry->min_col;
  } else
    DocTreeMake(doc,path);
  DocPrint(doc,"%s",buf);
  if (write && DrvIsWritable(*doc->filename.name))
    DocWrite(doc);
  if (unlock_doc)
    DocUnlock(doc);
  Free(buf);
  return res;
}

public Bool DocTreeWrite(CDoc *doc,U8 *path,Bool write=TRUE,U8 *fmt,...)
{//Rewrite doc tree branch.
  return DocTreeWriteJoin(doc,path,write,fmt,argc,argv);
}

public Bool DocTreeAppend(CDoc *doc,U8 *path,Bool write=TRUE,U8 *fmt,...)
{//Append to doc tree branch.
  return DocTreeAppendJoin(doc,path,write,fmt,argc,argv);
}

public Bool DocTreeFWrite(U8 *name,U8 *path,U8 *fmt,...)
{//Rewrite doc tree branch in file.
  CDoc *doc=DocRead(name);
  Bool res=DocTreeWriteJoin(doc,path,TRUE,fmt,argc,argv);
  DocDel(doc);
  return res;
}

public Bool DocTreeFAppend(U8 *name,U8 *path,U8 *fmt,...)
{//Append to doc tree branch in file.
  CDoc *doc=DocRead(name);
  Bool res=DocTreeAppendJoin(doc,path,TRUE,fmt,argc,argv);
  DocDel(doc);
  return res;
}

#help_index "DolDoc/Compiler;Compiler"
public I64 ExeDoc(CDoc *doc,I64 ccf_flags=0)
{//JIT Compile and execute a document.
  I64 res;
  Bool okay=TRUE,unlock_doc=DocLock(doc);
  CCmpCtrl *cc=CmpCtrlNew(,ccf_flags|CCF_DONT_FREE_BUF);
  if (Fs->last_cc!=&Fs->next_cc)
    cc->opts=Fs->last_cc->opts;
  QueIns(cc,Fs->last_cc);
  LexAttachDoc(cc,,doc);
  try {
    Lex(cc);
    res=ExeCmdLine(cc);
  } catch {
    if (Fs->except_ch=='Compiler' || Fs->except_ch=='Break') {
      Fs->catch_except=TRUE;
      okay=FALSE;
      res=0;
    }
  }
  QueRem(cc);
  if (okay)
    CmpCtrlDel(cc); //TODO: can crash
  if (unlock_doc)
    DocUnlock(doc);
  return res;
}

#help_index "DolDoc/Tree;DolDoc/Compiler;Compiler"
public I64 DocTreeExe(CDoc *doc,U8 *path)
{//Execute doc tree branch.
  CDoc *doc2;
  Bool unlock_doc=DocLock(doc);
  CDocEntry *tree_branch,*start_indent,*end_indent;
  I64 res=0;
  if (DocTreeFind(doc,path,&tree_branch,&start_indent,&end_indent)) {
    doc2=DocCopy(doc,tree_branch,end_indent);
    res=ExeDoc(doc2);
    DocDel(doc2);
  }
  if (unlock_doc)
    DocUnlock(doc);
  return res;
}

public I64 DocTreeFExe(U8 *name,U8 *path)
{//Execute doc tree branch in file.
  I64 res;
  CDoc *doc=DocRead(name);
  res=DocTreeExe(doc,path);
  DocDel(doc);
  return res;
}