/*{{{}}}*/
/*{{{  #includes*/
#ifdef CONFIG_H
#   include "config.h"
#endif

#include <sys/types.h>
#include <ctype.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define READFOLDS_C
#define I_DISPLAY_C
#define I_FINDS_C
#define I_FOLDING_C
#define I_GETTK_C
#define I_GETMSG_C
#define I_KEYBOARD_C
#define I_LOOP_C
#define I_MAIN_C
#define I_MESSAGES_C
#define I_MISC_C
#define I_ORIEDT_C
#define I_PROMPT_C
#define I_SCREEN_C
#define I_SHELL_C
#define I_SIGNALS_C
#define I_VIRTUAL_C
#define I_WRITEF_C

#include "origami.h"
#include <lib/ori_add_lib.h>
/*}}}  */

/*{{{  variables*/
private boolean lang_def;
private boolean tds_crash;
private FILE *rf_tdsfile;
private element *rf_current, *rf_trailing;
private int rf_new_indent;
private unsigned char *rf_dsp_line;
private unsigned char *rf_end_dsp_line;
private int hash_space_leave;
public struct f_sz file_io_size;
/*}}}  */

/*{{{  insert_file*/
/*{{{  complain*/
private void complain
 ( msgtyp const x,
   element const * const e,
   element const * const c
 )
{
  unsigned char pos[MSG_LENGTH+32];
  int line;

  if (c && e && (line=line_no(e,c)))
     ustrcpy(pos,get_msg(M_Y,line));
  else
     pos[0]='\0';
  warn_message(get_msg(x,pos));
}
/*}}}  */
/*{{{  node handling*/
/*{{{  create_node*/
private void create_node(boolean const txt)
{ element *pt;

  pt=proc_new_element();
  set_data(pt,rf_dsp_line,txt);
  if (!rf_trailing) pt->indent = rf_new_indent;
  join_links(rf_current, pt);
  rf_current = pt;
}
/*}}}  */
/*{{{  connect_fold*/
private void connect_fold(void)
{
  static element *rf_S;

  if (rf_trailing)
   { rf_S = rf_trailing;
     join_links(rf_current, rf_S->x.fold.other_end);
     rf_current = rf_S->x.fold.other_end;
     rf_current->x.fold.other_end = rf_S;
     rf_trailing = rf_S->x.fold.data;
     rf_S->x.fold.data = rf_S->next;
     rf_S->x.fold.other_end = rf_current;
     set_linetyp(*rf_S,get_linetyp(*rf_current));   /*start*/
     /* _fold or _filed */
     current_ind -= rf_S->x.fold.UU.U1.indent;
     set_linetyp(*rf_current,END_FOLD);
     rf_current = rf_S;
   }
}
/*}}}  */
/*{{{  start_a_fold*/
private void start_a_fold(linetyp const start_,int const indent)
{
  set_data(rf_current,rf_dsp_line,False);
  rf_current->x.fold.UU.U1.indent = indent;
  current_ind += indent;
  rf_current->x.fold.data = rf_trailing;
  rf_trailing = rf_current;
  rf_current->x.fold.other_end=proc_new_element();
  set_linetyp(*(rf_current->x.fold.other_end),start_);
}
/*}}}  */
/*}}}  */
/*{{{  read_tds*/
/*{{{  read_stream*/
private void read_stream(int lg)
{
  if (lg>LINELEN)
     tds_crash=True;
  else
   { fread((char*)rf_dsp_line,(size_t)1,(size_t)lg,rf_tdsfile);
     file_io_size.c+=lg;
     rf_dsp_line[lg]=0;
   }
  if (feof(rf_tdsfile))
     tds_crash=True;
  if (tds_crash)
     rf_dsp_line[0]='\0';
}
/*}}}  */
/*{{{  read_tag*/
private int read_tag(int * const vp)
{
  int tag,value;

  value = 0;
  do
   /*{{{  read part of the tag*/
   {
     /*{{{  handle breaks and errors*/
     if (aborted)
        tds_crash=True;
     if (tds_crash)
      { tag= -1;
        value=0;
        break;
      }
     /*}}}  */
     /*{{{  get and decode one byte*/
     { int x;

       if ((x=getc(rf_tdsfile))==EOF)
        /*{{{  use eof-tag*/
        { tag=2;
          value=1;
          break;
        }
        /*}}}  */
       /*{{{  get tag and combine old and new value*/
       tag=(unsigned char)(x>>6);
       value=(value<<6)+((unsigned char)x&63);
       /*}}}  */
     }
     /*}}}  */
     file_io_size.c++;
   }
   /*}}}  */
  while (tag == 3);
  if (vp)
     *vp=value;

  return(tag);
}
/*}}}  */
/*{{{  read_string*/
private void read_string(void)
{
  if (tds_crash)
     rf_dsp_line[0]='\0';
  else
   { int value;

     if (read_tag(&value)!=0)
        ustrcpy(rf_dsp_line,i_to_ua(value));
     else
        read_stream(value);
   }
}
/*}}}  */
/*{{{  read_a_list*/
private void read_fold(int,boolean);

private void read_a_list(int indent)
{ int tag, value;

  if (!tds_crash)
     do
      { switch((tag=read_tag(&value)))
         {
           /*{{{  0 = record*/
           case 0:
            { read_stream(value);
              file_io_size.l++;
              create_node(False);
              break;
            }
           /*}}}  */
           /*{{{  2 = function*/
           case 2:
            { switch (value)
               {
                 /*{{{  2 = startfold*/
                 case 2: { read_fold(indent, False);break; }
                 /*}}}  */
                 /*{{{  4 = startfiled*/
                 case 4: { read_fold(indent, True);break; }
                 /*}}}  */
               }
              break;
            }
           /*}}}  */
         }
      }
     while ((tag != 2 || value != 1) && !tds_crash);
}
/*}}}  */
/*{{{  read_fold*/
private void read_fold(int indent,boolean filed)
{
  int t, c, extra_indent;

  read_tag((int*)0);   /*startlist*/
  read_string();
  read_tag(&t);
  read_tag(&c);
  read_tag(&extra_indent);
  read_tag((int*)0);   /*should be endlist*/
  file_io_size.l++;
  create_node(False);
  if (filed)
   { start_a_fold(START_FILED, extra_indent);
     read_string();
     rf_dsp_line[_POSIX_PATH_MAX]='\0';
     file_io_size.l++;
     set_data(rf_trailing->x.fold.other_end,rf_dsp_line,False);
   }
  else
   { start_a_fold(START_FOLD, extra_indent);
     read_a_list(indent + extra_indent);
   }
  read_tag((int*)0);   /*endfold or endfiled*/
  file_io_size.l++;
  connect_fold();
  rf_current->x.fold.UU.U1.type = t;
  rf_current->x.fold.UU.U1.contents = c;
}
/*}}}  */

private boolean read_tds(unsigned char const * const name)
{
  boolean ret;
  FILE *f;

  ret=False;
  tds_crash=False;
  if (rf_tdsfile)
   /*{{{  test, if file is in tds format starts with A0* 80*/
   { f=rf_tdsfile;
     if
      /*{{{  file in tds format(starts with A0* 80)*/
      (    read_tag((int*)&tds_crash)==2
        && tds_crash==0
        && (rf_tdsfile=fopen((char*)name,(char*)"rb"))
      )
      /*}}}  */
      /*{{{  close and read tds*/
      { fclose(f);
        message(get_msg(F_LOADING,name));
        /*{{{  init tds read*/
        tds_crash=False;
        /*{{{  init file_io_size*/
        file_io_size.c=0;
        file_io_size.l=0;
        /*}}}  */
        bd.m.dialect.typ = F_C_TDS;
        /*}}}  */
        read_a_list(0);
        if (tds_crash || fgetc(rf_tdsfile)!=EOF)
         /*{{{  error*/
           complain(M_TDS_CRASH,(element*)0,(element*)0);
         /*}}}  */
        else
         /*{{{  show size of read data*/
           message
            ( get_msg
               ( F_SIZE_FILE,
                 name,
                 "read",
                 file_io_size.l,
                 file_io_size.c
               )
            );
         /*}}}  */
        if (fclose(rf_tdsfile))
           err_message(M_CLOSE_FAILED,name);
        ret=True;
      }
      /*}}}  */
     else
      /*{{{  seek to start*/
      { fseek(f,(off_t)0,SEEK_SET);
        rf_tdsfile=f;
      }
      /*}}}  */
   }
   /*}}}  */

  return(ret);
}
/*}}}  */
/*{{{  ascii helps*/
/*{{{  check_hash_stay*/
private int check_hash_stay(unsigned char const *s)
{
  int r=0;

  if (*s=='#')
     for(;*++s==' ';r++);

  return(r);
}
/*}}}  */
/*{{{  type allowed mark-extension*/
typedef enum
 { S_S_EX=0,  /* "  "         */
   AF_S_EX,   /* "A " or "F " */
   FS_S_EX    /* "F " or "  " */
 } m_ex;
/*}}}  */
/*{{{  find_dialect*/
private boolean find_dialect
 ( unsigned char const * ss,
   unsigned char const * const es
 )
{ f_c_types i;
  int lg;
  language_comment *dp;

  while (*ss==' ') ss++;
  if (!*ss) return(True);
  lg=es-ss;
  for (i=F_C_NONE,dp=dialects; i<F_C_USER; i++,dp++)
     if
      (    (lg==dp->lg.start)
        && !strncmp((char*)dp->txt.start,(char*)ss,lg)
      )
      { bd.m.dialect.typ=i;
        hash_space_leave=check_hash_stay(ss);
        return(True);
      }
  return(False);
}
/*}}}  */
/*{{{  test_mark*/
private int test_mark
 ( unsigned char const * const str,
   m_ex const ex,
   boolean * const dirty_fold
 )
{ int i;
  int the_pos;
  unsigned char *s;

  if (!(s=ustrstr(rf_dsp_line,str)))
     return(0);
  the_pos=1+(s-rf_dsp_line);
  if (!the_pos) return(0);
  if (lang_def)
   /*{{{  handle the dialect*/
   { if (!find_dialect(rf_dsp_line,rf_dsp_line+the_pos-1))
      { *dirty_fold=True;return(0); }
     dialects[F_C_USER].lg.start=dialects[bd.m.dialect.typ].lg.start;
     dialects[F_C_USER].lg.end  =dialects[bd.m.dialect.typ].lg.end;
     lang_def=False;
   }
   /*}}}  */
  s=rf_dsp_line+the_pos-1;
  /*{{{  check start-comment*/
  /*{{{  check leading comment string*/
  if
   (    dialects[F_C_USER].lg.start>=the_pos
     || (   dialects[F_C_USER].lg.start
         && strncmp
             ( (char*)(s-dialects[F_C_USER].lg.start),
               (char*)dialects[bd.m.dialect.typ].txt.start,
               dialects[F_C_USER].lg.start
             )
        )
   )
   { *dirty_fold=True;
     return(0);
   }
  /*}}}  */
  /*{{{  check leading spaces/tabs*/
  { unsigned char *t=rf_dsp_line;
    int x=the_pos-1-(dialects[F_C_USER].lg.start);

    while (x && *t==' ') { x--;t++; }
    if (x) { *dirty_fold=True;return(0); }
  }
  /*}}}  */
  /*}}}  */
  s+=FOLD_TAG_LENGTH;
  /*{{{  check, if mark is followed by correct 2 letters*/
  if (*s && ex!=S_S_EX)
   { if (*(s+1) && (*(s+1)!=' ')) { *dirty_fold=True;return(0); }
     switch (*s)
      { case 'a':
        case 'A':
           if (ex!=AF_S_EX) { *dirty_fold=True;return(0); }
           *s='A';
           break;
        case 'f':
        case 'F':
           if (ex==S_S_EX) { *dirty_fold=True;return(0); }
           *s='F';
        case ' ':
           break;
        default:
           *dirty_fold=True;
           return(0);
      }
   }
  /*}}}  */
  i=rf_end_dsp_line-s;
  /*{{{  check end-comment*/
  if
   (   dialects[F_C_USER].lg.end
    && (   (i<dialects[F_C_USER].lg.end)
        || ustrcmp
            ( (rf_end_dsp_line-dialects[F_C_USER].lg.end),
              dialects[bd.m.dialect.typ].txt.end
            )
        )
   )
   { *dirty_fold=True;
     return(0);
   }
  /*}}}  */
  *(rf_end_dsp_line-=dialects[F_C_USER].lg.end)='\0';
  rf_dsp_line=(unsigned char*)s;
  return(the_pos-dialects[F_C_USER].lg.start);
}
/*}}}  */
/*}}}  */

public void insert_file
 ( element const * const file_ptr,
   element * const prec_ptr,
   element * const next_ptr,
   boolean const remember_file,
   element const * const start_count,
   boolean * const dir_read,
   boolean * const comment_line_ptr,
   FILE * const read_pipe
 )
{
  /*{{{  local variables*/
  FILE *tstorefile;
  boolean filetype_is_dir;
  unsigned char rf_filename[_POSIX_PATH_MAX+1];
  unsigned char dsp_line[LINELEN + _POSIX_PATH_MAX + 3];
  element node;
  element *nf_ptr=0;
  element *ic_ptr=0;
  element *df_ptr=0;
  element *ls_ptr=0;
  element *es_ptr=0;
  /*}}}  */

  /*{{{  prepare some variables (pointers, indentation..))*/
  bd.m.dialect.typ=F_C_NONE;
  if (dir_read) *dir_read=False;
  if (comment_line_ptr) *comment_line_ptr=False;
  rf_filename[0]=0;
  node.next=0;
  set_linetyp(node,NOT_FOLD);
  rf_current= &node;
  rf_new_indent=next_ptr->indent;
  if (test_linetyp(*next_ptr,START_OPEN_FOLD))
     rf_new_indent-=next_ptr->x.fold.UU.U1.indent;
  rf_trailing = 0;
  rf_dsp_line = dsp_line;
  current_ind = 0;
  /*}}}  */
  /*{{{  get and check filename*/
  if (!read_pipe)
   { if (!file_ptr)
      /*{{{  use new filename*/
       if (filearg)
        /*{{{  use given one*/
        { ustrcpy(rf_filename,filearg);
          filearg=0;
        }
        /*}}}  */
       else
        {
          /*{{{  maybe use pipename*/
#          ifdef PIPE_FILENAME
             if (start_pipe)
                ustrcpy(rf_filename,(unsigned char*)PIPE_FILENAME);
             else
#          endif
          /*}}}  */
          /*{{{  else prompt or use default*/
          if (running)
             fileprompt(rf_filename);
          else
           /*{{{  prompt and/or default files*/
#            ifdef DEFAULT_FILE
                ustrcpy(rf_filename,(unsigned char*)DEFAULT_FILE);
#            else
              { fileprompt(rf_filename);
#               if defined(ALWAYS_START) && defined(OS_NULL_DEVICE)
                   if (!rf_filename[0] && !aborted)
                      ustrcpy(rf_filename,(unsigned char*)OS_NULL_DEVICE);
#               endif
              }
#             endif
           /*}}}  */
          /*}}}  */
        }
      /*}}}  */
     else
      /*{{{  use file-fold-stack name*/
        ustrcpy(rf_filename,get_data(file_ptr));
      /*}}}  */
     if (*rf_filename == '\0' && remember_file)
        exit_origami(bd.m.file_changed_status!=unchanged_file?r_file_nok:r_ok,get_msg(M_BYE));
     if
      (    access((char *)rf_filename,F_OK)==0
        && access((char *)rf_filename,W_OK)==-1
      )
        bd.m.read_only=True;
   }
  title_op(CHGXY);
  /*}}}  */
  if (!aborted)
   { if (!executing_macro)
        enable_abort();
     /*{{{  open file*/
     rf_tdsfile = 0;
     if (read_pipe==(FILE*)0)
      { filetype_is_dir=is_dir((FILE*)0,(char*)rf_filename);
        if (filetype_is_dir)
           tstorefile=0;
        else
           if ((tstorefile=fopen((char *)rf_filename,(char*)"r")))
              rf_tdsfile=tstorefile;
           else
              err_message(M_CANTOPEN,rf_filename);
      }
     /*}}}  */
     /*{{{  read the file*/
     if (read_pipe || bd.e.fold_in || !read_tds(rf_filename))
      /*{{{  read ascii-file*/
      {
        /*{{{  vars for ascii read*/
        boolean null_found=False;
        boolean indent_crash=False;
        boolean dirty_fold=False;
        boolean line_split=False;
        boolean end_space=False;
        int cut_lg=0;
        int f_depth=0;
        unsigned char cut_char=0;
        unsigned char fold_type_str[FOLD_TAG_LENGTH+FOLD_TAG_LENGTH+1];
#        ifdef DIREDT
          boolean reading_dir=False;
          boolean reset_nonfold=False;
#        endif
        /*}}}  */

        message(get_msg(F_LOADING,read_pipe?M_PSTR:rf_filename));
        /*{{{  enable language definition parsing*/
        lang_def=True;
        /*}}}  */
        /*{{{  init file_io_size*/
        file_io_size.c=0;
        file_io_size.l=0;
        /*}}}  */
        /*{{{  init local vars*/
        ustrcpy(fold_type_str,bd.f.str.open_f);
        ustrcat(fold_type_str,bd.f.str.close_f);
        bd.m.dialect.typ = F_C_NONE;
        /*}}}  */
        /*{{{  handle directory editing*/
        if (read_pipe)
           tstorefile=read_pipe;
        else
           if (!rf_tdsfile && filetype_is_dir)
            {
#        ifdef DIREDT
              if (rf_filename[0])
               /*{{{  open pipe for dirfold*/
               { unsigned char *match,*nomatch;
                 histories old_default;

                 /*{{{  get values for diredit filters*/
                 old_default=overwrite_history_id;
                 overwrite_history_id=match_history;
                 access_history(0,match_history,False,(int*)0,&match);
                 overwrite_history_id=nomatch_history;
                 access_history(0,nomatch_history,False,(int*)0,&nomatch);
                 overwrite_history_id=old_default;
                 /*}}}  */
                 /*{{{  open pipe*/
                 force_sh=True;
                 tstorefile=open_shell
                             ( DIR_CMD
                                ( rf_filename,
                                  ocl_var[var_dired].v,
                                  match,
                                  nomatch
                                ),
                               "r"
                             );
                 /*}}}  */
                 reading_dir=True;
                 if (bd.e.fold_in)
                  { bd.e.fold_in=False;
                    reset_nonfold=True;
                  }
                 if (dir_read)
                    *dir_read=True;
               }
               /*}}}  */
              else
#        endif
                 tstorefile=0;
            }
        /*}}}  */
        if (tstorefile)
         /*{{{  read the file*/
         { boolean look_for_att=False;

           hash_space_leave=0;
           while (!aborted)
            { boolean hash_moved=False;
              boolean last_space;
              boolean check_line;

              /*{{{  read the line*/
              rf_dsp_line=dsp_line;
              *rf_dsp_line=cut_char;
              if
               ( !(fgets((char *)rf_dsp_line+cut_lg,
                         LINELEN+2-bd.f.enter_spaces-rf_new_indent-cut_lg,
                         tstorefile))
               )
                 if (cut_lg)
                    *(rf_dsp_line+cut_lg)='\n';
                 else
                    break;
              /*}}}  */
              /*{{{  check length, cut on '\0', check trailing space/tab*/
              { int line_lg;

                /*{{{  scan line for \n..*/
                { int i;
                  unsigned char *s;

                  /*{{{  move s behind last valid char on the line*/
                  for
                   ( s=rf_dsp_line,
                     i=LINELEN-bd.f.enter_spaces-rf_new_indent,
                     cut_lg=0
                   ;
                   ; i--,
                     s++
                   )
                   { switch (*s)
                      { case '\0':
                           null_found=True;
                        case '\n':
                           break;
                        default:
                           if (i==0)
                            { cut_char= *s;
                              cut_lg=1;
                              line_split=True;
                              break;
                            }
                           continue;
                      }
                     break;
                   }
                  /*}}}  */
                  *(rf_end_dsp_line=s)=0;
                  line_lg=rf_end_dsp_line-rf_dsp_line;
                  last_space=(    line_lg
                               && (    rf_end_dsp_line[-1]==' '
                                    || rf_end_dsp_line[-1]=='\t'
                                  )
                             );
                }
                /*}}}  */
                /*{{{  handle read statistics*/
                file_io_size.c+=line_lg+1;
                file_io_size.l++;
                /*}}}  */
              }
              /*}}}  */
              if (!bd.e.fold_in && lang_def)
               /*{{{  check if current line is a comment-description*/
               { unsigned char *s,*d;


                 for (s=rf_dsp_line;*s==' ';s++);
                 if ((d=ustrstr(s,fold_type_str)))
                  /*{{{  user defined comment type*/
                  { boolean ok;
                    int marker_pos;

                    marker_pos=d-s;
                    /*{{{  set dialect start*/
                    if
                     ((ok=(   (dialects[F_C_USER].lg.start=marker_pos)
                           <= FOLD_COMMENT_LENGTH
                          )
                     ))
                     { d=dialects[F_C_USER].txt.start;
                       while (marker_pos--) *d++ = *s++;
                       *d='\0';
                     }
                    /*}}}  */
                    if (ok)
                     /*{{{  set dialect end*/
                     { s+=(FOLD_TAG_LENGTH+FOLD_TAG_LENGTH);
                       if
                        ((ok=    ((dialects[F_C_USER].lg.end=rf_end_dsp_line-s)
                              <= FOLD_COMMENT_LENGTH)
                        ))
                          ustrcpy(dialects[F_C_USER].txt.end,s);
                     }
                     /*}}}  */
                    if (ok)
                     /*{{{  set this language and continue*/
                     { f_c_types i;
                       language_comment *dp;

                       bd.m.dialect.typ = F_C_USER;
                       for (i=0,dp=dialects;i<F_C_USER;dp++)
                          if
                           /*{{{  comments do not match*/
                           (   ustrcmp(dp->txt.start,
                                       dialects[F_C_USER].txt.start)
                            || ustrcmp(dp->txt.end,
                                       dialects[F_C_USER].txt.end)
                           )
                           /*}}}  */
                           /*{{{  test next*/
                             i++;
                           /*}}}  */
                          else
                           /*{{{  set to this one*/
                           { bd.m.dialect.typ=i;
                             break;
                           }
                           /*}}}  */
                       lang_def=False;
                       if (comment_line_ptr) *comment_line_ptr=True;
                       hash_space_leave=check_hash_stay(dialects[F_C_USER].txt.start);
                       continue;
                     }
                     /*}}}  */
                    else
                     /*{{{  reset length of user comment strings*/
                     { dialects[F_C_USER].lg.start=
                       dialects[F_C_USER].lg.end  =0;
                     }
                     /*}}}  */
                  }
                  /*}}}  */
               }
               /*}}}  */
              if (rf_dsp_line[0]=='#')
               /*{{{  handle hash shift*/
               {
                 /*{{{  maybe switch on hash shift*/
                 if
                  (    !bd.m.hash_shift
                    && (    current_ind
                         || (    !lang_def
                              && dialects[bd.m.dialect.typ].txt.start[0]=='#'
                            )
                       )
                  )
                  { bd.m.hash_shift=True;
                    title_op(CHGTITLE);
                  }
                 /*}}}  */
                 if (bd.m.hash_shift)
                  /*{{{  move # to first non-space in line*/
                  { unsigned char *s;
                    int i;

                    hash_moved=True;
                    for (*(s=rf_dsp_line)=' ',i=0;*++s==' ';i++);
                    if (i>hash_space_leave)
                       i= -1-hash_space_leave;
                    else
                       i= -1;
                    s[i]='#';
                  }
                  /*}}}  */
               }
               /*}}}  */
              /*{{{  cut current indentation*/
              { int i;

                for (i=current_ind;;i--,rf_dsp_line++)
                   if (i)
                    { if (*rf_dsp_line=='\0')
                         break;
                      else if (*rf_dsp_line!=' ')
                       { indent_crash=True;
                         break;
                       }
                    }
                   else
                      break;
              }
              /*}}}  */
              /*{{{  maybe handle filename- or close-fold-lines*/
              if ((check_line=(!bd.e.fold_in && rf_dsp_line[0])))
               { if
                  (    f_depth
                    && test_mark(bd.f.str.close_f,S_S_EX,&dirty_fold)>0
                  )
                  /*{{{  close-fold*/
                  { f_depth--;
                    connect_fold();
                    look_for_att=False;
                    continue;
                  }
                  /*}}}  */
                 else if
                  (    look_for_att
                    && test_mark(bd.f.str.file_f,AF_S_EX,&dirty_fold)>0
                  )
                  /*{{{  name of filed or attributes*/
                  { boolean ff=(rf_dsp_line[0]=='F');

                    if (*(++rf_dsp_line)) rf_dsp_line++;
                    if (ff)
                     /*{{{  filed*/
                     { if (rf_trailing)
                        { rf_dsp_line[_POSIX_PATH_MAX]='\0';
                          set_data(rf_trailing->x.fold.other_end,rf_dsp_line,False);
                        }
                       continue;
                     }
                     /*}}}  */
                    else
                     /*{{{  attibutes*/
                     { if (rf_trailing)
                        { unsigned char *s=rf_dsp_line;

                          /*{{{  set type*/
                          rf_trailing->x.fold.UU.U1.type
                             =(unsigned char)atoi((char *)rf_dsp_line);
                          /*}}}  */
                          /*{{{  set contents*/
                          while (*s==' ') s++;
                          while (*s && *s!=' ') s++;
                          rf_trailing->x.fold.UU.U1.contents=
                             (unsigned char)atoi((char *)s);
                          /*}}}  */
                          /*{{{  set data*/
                          while (*s==' ')
                             s++;
                          while (isdigit(*s))
                             s++;
                          if (*s==' ')
                             s++;
                          if (*s)
                           { s[_POSIX_PATH_MAX]='\0';
                             set_data(rf_trailing->x.fold.other_end,s,False);
                           }
                          /*}}}  */
                        }
                       continue;
                     }
                     /*}}}  */
                  }
                  /*}}}  */
               }
              /*}}}  */
              { int the_pos=
                 /*{{{  start fold line?*/
                 ( check_line
                     ? test_mark(bd.f.str.open_f,FS_S_EX,&dirty_fold)
                     : 0
                 );
                 /*}}}  */

                create_node(the_pos<=0);
                if (the_pos>0)
                 /*{{{  start fold*/
                 { boolean ff=(rf_dsp_line[0]=='F');

                   lang_def=False;
                   if (*rf_dsp_line && *(++rf_dsp_line)) rf_dsp_line++;
                   f_depth++;
                   start_a_fold(ff?START_FILED:START_FOLD,the_pos-1);
                   look_for_att=True;
                 }
                 /*}}}  */
                else
                 /*{{{  text line*/
                 { if (hash_moved)
                    /*{{{  move hash to fold-start*/
                    { unsigned char *s=rf_dsp_line;

                      while (*s==' ') s++;
                      if (*s=='#')
                       /*{{{  hash still there, so move back to fold-indentation*/
                       { *s=' ';
                         rf_dsp_line[0]='#';
                         set_data(rf_current,rf_dsp_line,True);
                       }
                       /*}}}  */
                    }
                    /*}}}  */
                   /*{{{  check trailling whitespace*/
                   if (rf_dsp_line[0] && !current_ind && last_space)
                      end_space=True;
                   /*}}}  */
                   look_for_att=False;
                 }
                 /*}}}  */
                /*{{{  maybe store the line near an read error*/
                if (dirty_fold && !df_ptr) df_ptr=rf_current;
                if (null_found && !nf_ptr) nf_ptr=rf_current;
                if (indent_crash && !ic_ptr) ic_ptr=rf_current;
                if (line_split && !ls_ptr) ls_ptr=rf_current;
                if (end_space && !es_ptr) es_ptr=rf_current;
                /*}}}  */
              }
            }
         }
         /*}}}  */
        /*{{{  check some error-situations on parsed file*/
        if (f_depth)
         /*{{{  autoclose and complain*/
         {
           /*{{{  rf_dsp_line=dsp_line=dialect_start f_depth M_ACF_MSG dialect_end*/
           ustrcpy
            ( dsp_line,
              get_msg(M_ACF_LINE,
                      dialects[bd.m.dialect.typ].txt.start,
                      f_depth,
                      bd.f.str.close_f,
                      dialects[bd.m.dialect.typ].txt.end)
            );
           rf_dsp_line=dsp_line;
           /*}}}  */
           do connect_fold(); while (--f_depth);
           /*{{{  add errormessage and complain*/
           create_node(False);
           complain(M_ACF_MSG,(element*)0,(element*)0);
           /*}}}  */
         }
         /*}}}  */
        /*}}}  */
        /*{{{  maybe reset bd.e.fold_in*/
#        ifdef DIREDT
          if (reset_nonfold) bd.e.fold_in=True;
#        endif
        /*}}}  */
        /*{{{  show size of read data*/
        if (read_pipe || tstorefile)
           message
            ( get_msg
               ( F_SIZE_FILE,
                 (read_pipe?M_PSTR:rf_filename),
                 "read",
                 file_io_size.l,
                 file_io_size.c
               )
            );
        else
           msg_message(M_NEW_FILE);
        /*}}}  */
        /*{{{  close the file*/
        if (   !read_pipe
            && tstorefile
            && (
#        ifdef DIREDT
                 reading_dir
                  ? close_shell(tstorefile)
                  :
#        endif
                    fclose(tstorefile)
               )
            )
              err_message(M_CLOSE_FAILED,rf_filename);
        /*}}}  */
      }
      /*}}}  */
     /*}}}  */
     if (!executing_macro)
        disable_abort();
   }
  /*{{{  link the read text to the edited text*/
  if (node.next)
   { join_links(prec_ptr, node.next);
     join_links(rf_current, next_ptr);
   }
  /*}}}  */
  /*{{{  filename must maybe moved to internal data*/
  if (remember_file)
   { if (!file_ptr) set_data(prec_ptr,rf_filename,False);
     set_data(next_ptr,rf_filename,False);
   }
  /*}}}  */
  /*{{{  some error messages for ascii read, put it after linking the read text*/
  if (df_ptr) complain(M_DIRTY_FOLD,df_ptr,start_count);
  if (nf_ptr) complain(M_NULL_CRASH,nf_ptr,start_count);
  if (ic_ptr) complain(M_INDENT_CRASH,ic_ptr,start_count);
  if (ls_ptr) complain(M_LONG_LINE_SPLIT,ls_ptr,start_count);
  if (es_ptr) complain(M_TRAIL_SPACE,es_ptr,start_count);
  /*}}}  */
}
/*}}}  */
