/* * FindFcbWithLine - find the fcb with the specified line */ vi_rc FindFcbWithLine( linenum lineno, file *cfile, fcb **fb ) { bool lastflag = false; fcb *tfcb, *ofcb; vi_rc rc; /* * are we looking for the last line? */ if( lineno < 0 ) { lastflag = true; lineno = MAX_LONG; } if( lineno < 1 ) { return( ERR_NO_SUCH_LINE ); } if( cfile == NULL ) { return( ERR_NO_FILE ); } /* * run through all possible fcb's */ tfcb = cfile->fcbs.head; if( tfcb == NULL ) { return( ERR_NO_SUCH_LINE ); } for( ;; ) { if( tfcb->end_line >= lineno ) { *fb = tfcb; FetchFcb( tfcb ); return( ERR_NO_ERR ); } ofcb = tfcb; tfcb = ofcb->next; if( tfcb == NULL ) { if( !cfile->bytes_pending ) { if( lastflag ) { *fb = ofcb; FetchFcb( ofcb ); return( ERR_NO_ERR ); } return( ERR_NO_SUCH_LINE ); } if( EditFlags.Verbose ) { Message1( "At line %l", ofcb->end_line ); } rc = ReadFcbData( cfile, NULL ); if( rc != ERR_NO_ERR && rc != END_OF_FILE ) { return( rc ); } tfcb = cfile->fcbs.tail; } } } /* FindFcbWithLine */
/* * GimmeNextLinePtr - get pointer to next line */ vi_rc GimmeNextLinePtr( file *cfile, fcb **cfcb, line **cline ) { vi_rc rc; fcb *ofcb; /* * get next line pointer; if not null, go back */ *cline = (*cline)->next; if( *cline != NULL ) { return( ERR_NO_ERR ); } /* * get next fcb pointer; if not null, get first line and go back */ *cfcb = (*cfcb)->next; if( *cfcb != NULL ) { FetchFcb( *cfcb ); *cline = (*cfcb)->lines.head; return( ERR_NO_ERR ); } /* * get next fcb if can; then get first line and go back */ if( cfile->bytes_pending ) { ofcb = cfile->fcbs.tail; rc = ReadFcbData( cfile ); if( rc > ERR_NO_ERR ) { return( rc ); } *cfcb = cfile->fcbs.tail; if( *cfcb != ofcb ) { while( (*cfcb)->prev != ofcb ) { *cfcb = (*cfcb)->prev; } } FetchFcb( *cfcb ); *cline = (*cfcb)->lines.head; return( ERR_NO_ERR ); } /* * no such line */ *cfcb = NULL; *cline = NULL; return( ERR_NO_MORE_LINES ); } /* GimmeNextLinePtr */
/* * createNewFile - create new file entry */ static vi_rc createNewFile( char *name, bool same_file ) { int height; window_id cw; info *tmp; vi_rc rc; /* * test that we can create this file */ tmp = NULL; if( !same_file ) { rc = FileExists( name ); if( !(rc == ERR_READ_ONLY_FILE || rc == ERR_NO_ERR || rc == ERR_FILE_EXISTS) ) { return( rc ); } } else { if( name != NULL ) { for( tmp = InfoHead; tmp != NULL; tmp = tmp->next ) { if( !strcmp( tmp->CurrentFile->name, name ) ) { break; } } } if( tmp == NULL ) { return( ERR_FILE_NOT_FOUND ); } if( tmp->CurrentFile->dup_count > MAX_DUPLICATE_FILES ) { return( ERR_WIND_NO_MORE_WINDOWS ); } } /* * get new window */ rc = NewWindow2( &cw, &editw_info ); if( rc != ERR_NO_ERR ) { return( rc ); } #ifdef __WIN__ if( !strncmp( name, "untitled", 8 ) ) { // better yet, pass normal/maximize flag to NewWindow2... ShowWindow( cw, SW_SHOWMAXIMIZED ); } #endif SetBorderGadgets( cw, EditFlags.WindowGadgets ); /* * get new file entry, and read the data */ if( same_file ) { CurrentFile = tmp->CurrentFile; CurrentFile->dup_count++; SetFileWindowTitle( CurrentWindow, CurrentInfo, true ); tmp = CurrentInfo; CurrentInfo = MemAlloc( sizeof( *CurrentInfo ) ); FTSRunCmds( name ); rc = ERR_NO_ERR; } else { bool crlf_reached; crlf_reached = false; tmp = CurrentInfo; CurrentInfo = MemAlloc( sizeof( *CurrentInfo ) ); FTSRunCmds( name ); height = editw_info.y2 - editw_info.y1 + 1; CurrentFile = FileAlloc( name ); rc = OpenFcbData( CurrentFile ); for( ; rc == ERR_NO_ERR; ) { rc = ReadFcbData( CurrentFile, &crlf_reached ); if( rc == ERR_NO_ERR && !CurrentFile->is_stdio ) { if( EditFlags.BreakPressed || !EditFlags.ReadEntireFile ) { if( CurrentFile->fcbs.tail->end_line > height ) { break; } } } } if( rc != ERR_NO_ERR && rc != ERR_FILE_NOT_FOUND && rc != END_OF_FILE ) { MemFree( CurrentInfo ); CurrentInfo = tmp; FileFree( CurrentFile ); CloseAWindow( cw ); return( rc ); } if( rc == ERR_FILE_NOT_FOUND ) { rc = NEW_FILE; EditFlags.NewFile = true; CurrentFile->write_crlf = FileSysNeedsCR( CurrentFile->handle ); #ifdef __UNIX__ CurrentFile->attr = PMODE_RW; #endif } else { rc = ERR_NO_ERR; CurrentFile->write_crlf = crlf_reached; EditFlags.NewFile = false; #ifdef __UNIX__ { struct stat sb; stat( name, &sb ); CurrentFile->attr = sb.st_mode & ~S_IFMT; } #endif } CurrentFile->check_readonly = true; } /* * create info entry */ CurrentPos.line = 0; CurrentPos.column = 1; VirtualColumnDesired = 1; LeftTopPos.line = 1; LeftTopPos.column = 0; if( !same_file ) { AllocateUndoStacks(); } AllocateMarkList(); CurrentWindow = cw; CurrentInfo->DuplicateID = CurrentFile->dup_count; CurrentInfo->CurrentWindow = cw; LangInit( CurrentInfo->fsi.Language ); #ifdef __WIN__ { window_data *wd; wd = DATA_FROM_ID( cw ); wd->info = CurrentInfo; } #endif DCCreate(); SetFileWindowTitle( CurrentWindow, CurrentInfo, true ); /* * set current file info */ CurrentFcb = CurrentFile->fcbs.head; CurrentLine = CurrentFcb->lines.head; if( EditFlags.LineNumbers ) { LineNumbersSetup(); } if( tmp != NULL ) { InsertLLItemAfter( (ss **)&InfoTail, (ss *)tmp, (ss *)CurrentInfo ); } else { AddLLItemAtEnd( (ss **)&InfoHead, (ss **)&InfoTail, (ss *)CurrentInfo ); } return( rc ); } /* createNewFile */