vi_rc Delete( range *r ) { vi_rc rc; // need to perform the actual delete if( r->line_based ) { rc = DeleteLineRange( r->start.line, r->end.line, SAVEBUF_FLAG ); DCDisplayAllLines(); LineDeleteMessage( r->start.line, r->end.line ); } else if( r->start.line != r->end.line ) { rc = Cut( r->start.line, r->start.column, r->end.line, r->end.column, true ); DCDisplayAllLines(); } else { GoToLineNoRelCurs( r->start.line ); rc = DeleteRangeOnCurrentLine( r->start.column, r->end.column, true ); } // move cursor to tl corner of region if( rc == ERR_NO_ERR ) { GoToLineNoRelCurs( r->start.line ); r->start.column += 1; if( r->start.column > CurrentLine->len ) { if( EditFlags.Modeless ) { r->start.column = CurrentLine->len + 1; } else { r->start.column = CurrentLine->len; } } GoToColumnOnCurrentLine( r->start.column ); EditFlags.Dotable = true; } else { rc = DO_NOT_CLEAR_MESSAGE_WINDOW; } return( rc ); } /* Delete */
/* * HideLineRange - hide/unhide a given line range */ vi_rc HideLineRange( linenum s, linenum e, bool unhide ) { vi_rc rc; bool hideval; fcb *cfcb; line *cline; char *st; linenum c; hideval = TRUE; if( unhide ) { hideval = FALSE; } for( c = s; c <= e; c++ ) { rc = CGimmeLinePtr( c, &cfcb, &cline ); if( rc != ERR_NO_ERR ) { return( rc ); } cline->u.ld.hidden = hideval; } DCDisplayAllLines(); if( unhide ) { st = "revealed"; } else { st = "hidden"; } Message1( "%l lines %s", e - s + 1, st ); EditFlags.Dotable = TRUE; return( DO_NOT_CLEAR_MESSAGE_WINDOW ); } /* HideLineRange */
/* * SetCurrentLine - reset current line after changes in current file structure */ vi_rc SetCurrentLine( linenum lineno ) { int text_lines; fcb *cfcb; line *cline; vi_rc rc; if( lineno <= 0 ) { lineno = 1; } rc = CGimmeLinePtr( lineno, &cfcb, &cline ); if( rc != ERR_NO_ERR ) { return( rc ); } CurrentLine = cline; CurrentFcb = cfcb; text_lines = WindowAuxInfo( current_window_id, WIND_INFO_TEXT_LINES ); if( lineno < LeftTopPos.line || lineno > (LeftTopPos.line + text_lines - 1) ) { LeftTopPos.line = lineno - text_lines / 2; } if( LeftTopPos.line < 1 ) { LeftTopPos.line = 1; } CheckCurrentColumn(); SetCurrentLineNumber( lineno ); UpdateStatusWindow(); SetWindowCursor(); DCDisplayAllLines(); return( ERR_NO_ERR ); } /* SetCurrentLine */
/* * DoDigit - process a digit typed in */ vi_rc DoDigit( void ) { vi_rc rc; if( LastEvent == '0' && RepeatDigits == 0 ) { LeftTopPos.column = 0; GoToColumnOK( 1 ); DCDisplayAllLines(); return( ERR_NO_ERR ); } if( RepeatDigits == MAX_REPEAT_STRING - 1 ) { DoneRepeat(); return( ERR_REPEAT_STRING_TOO_LONG ); } if( repeatWindow == NO_WINDOW && EditFlags.RepeatInfo ) { rc = NewWindow2( &repeatWindow, &repcntw_info ); if( rc != ERR_NO_ERR ) { DoneRepeat(); return( rc ); } WindowTitle( repeatWindow, "Repeat Count" ); } RepeatString[RepeatDigits++] = LastEvent; RepeatString[RepeatDigits] = 0; if( repeatWindow != NO_WINDOW ) { UpdateRepeatString( RepeatString ); } return( GOT_A_DIGIT ); } /* DoDigit */
/* * SetCurrentColumn - set CurrentPos.column, positioning LeftTopPos.column nicely */ vi_rc SetCurrentColumn( int newcol ) { long oldpos; int text_cols; oldpos = CurrentPos.column - LeftTopPos.column; if( newcol <= 0 ) { newcol = 1; } text_cols = WindowAuxInfo( current_window_id, WIND_INFO_TEXT_COLS ); if( oldpos < 0 || oldpos >= text_cols ) { LeftTopPos.column = newcol - SCROLL_HLINE - 1; } else { LeftTopPos.column = newcol - oldpos - 1; } if( LeftTopPos.column < 0 ) { LeftTopPos.column = 0; } CurrentPos.column = newcol; CheckCurrentColumn(); UpdateCursorDrag(); VarAddRandC(); PositionHorizontalScrollThumb( current_window_id, LeftTopPos.column ); UpdateStatusWindow(); SetWindowCursor(); DCDisplayAllLines(); return( ERR_NO_ERR ); } /* SetCurrentColumn */
vi_rc ChangeCase( range *r ) { linenum curr; vi_rc rc; long total; const char *msg; UndoReplaceLines( r->start.line, r->end.line ); if( r->start.line == r->end.line ) { rc = changeOneLine( r->start.line, r->start.column, r->end.column ); msg = MSG_CHARACTERS; total = r->end.column - r->start.column + 1; } else { rc = changeToEndOfLine( r->start.line, r->start.column ); if( rc != ERR_NO_ERR ) { return( rc ); } for( curr = r->start.line + 1; curr < r->end.line; curr++ ) { rc = changeToEndOfLine( curr, 0 ); if( rc != ERR_NO_ERR ) { return( rc ); } } rc = changeOneLine( r->end.line, 0, r->end.column ); msg = MSG_LINES; total = r->end.line - r->start.line + 1; } EditFlags.Dotable = true; DCDisplayAllLines(); Message1( "case toggled for %l %s", total, msg ); Modified( true ); return( DO_NOT_CLEAR_MESSAGE_WINDOW ); } /* ChangeCase */
/* * insertTextOnOtherLine - open up a different line */ static vi_rc insertTextOnOtherLine( insert_dir type ) { char *buffx; int i, j; linenum a, b; bool above_line = FALSE; vi_rc rc; rc = ModificationTest(); if( rc != ERR_NO_ERR ) { return( rc ); } /* * special case: no data in file */ if( CurrentFcb->nullfcb ) { return( InsertTextAfterCursor() ); } /* * get line deletion and undo crap */ a = b = CurrentPos.line + 1; if( type == INSERT_BEFORE ) { a--; b--; above_line = TRUE; } /* * set up for undo */ StartUndoGroup( UndoStack ); Modified( TRUE ); StartUndoGroup( UndoStack ); UndoInsert( a, a, UndoStack ); currLineRepUndo = FALSE; /* * add extra line, and spaces if needed. */ if( EditFlags.AutoIndent ) { buffx = StaticAlloc(); i = GetAutoIndentAmount( buffx, 0, above_line ); AddNewLineAroundCurrent( buffx, i, type ); StaticFree( buffx ); j = i + 1; } else { AddNewLineAroundCurrent( NULL, 0, type ); j = 1; } GoToLineRelCurs( b ); GoToColumn( j, CurrentLine->len + 1 ); DCDisplayAllLines(); continueInsertText( CurrentPos.column, FALSE ); return( ERR_NO_ERR ); } /* insertTextOnOtherLine */
/* * LocateCmd - parse a locate command (format: locate r,c[,len]) */ vi_rc LocateCmd( const char *data ) { char tmp[MAX_STR]; linenum r; int c; int len; #ifdef __WIN__ if( BAD_ID( current_window_id ) ) { return( ERR_INVALID_LOCATE ); } #endif data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_INVALID_LOCATE ); } r = atol( tmp ); data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_INVALID_LOCATE ); } c = atoi( tmp ); // real selection length while( isspace( *data ) ) { data++; } len = 0; if( *data != 0 ) { data = GetNextWord1( data, tmp ); if( *tmp == '\0' ) { return( ERR_INVALID_LOCATE ); } len = atoi( tmp ); } GoToLineNoRelCurs( r ); c = RealColumnOnCurrentLine( c ); GoToColumnOnCurrentLine( c + len ); #ifdef __WIN__ // likely only called by dde, which doesn't use event loop, // so must ensure cache ok and set cursor here DCInvalidateAllLines(); DCDisplayAllLines(); DCUpdate(); SetWindowCursor(); SetWindowCursorForReal(); #endif if( len > 0 ) { SetSelRegionCols( CurrentPos.line, c, c + len - 1 ); } return( ERR_NO_ERR ); } /* LocateCmd */
/* * LineNumbersSetup - set up line number window */ vi_rc LineNumbersSetup( void ) { int j; int x1, x2; window_info wi; vi_rc rc; if( EditFlags.LineNumbers ) { if( editw_info.x2 - editw_info.x1 < EditVars.LineNumWinWidth ) { return( ERR_CANNOT_OPEN_LINENUM_WIND ); } if( !linenumw_info.has_border ) { j = 1; } else { j = 0; } memcpy( &wi, &linenumw_info, sizeof( window_info ) ); x1 = WindowAuxInfo( CurrentWindow, WIND_INFO_X1 ); x2 = WindowAuxInfo( CurrentWindow, WIND_INFO_X2 ); wi.y1 = WindowAuxInfo( CurrentWindow, WIND_INFO_Y1 ); wi.y2 = WindowAuxInfo( CurrentWindow, WIND_INFO_Y2 ); if( !EditFlags.LineNumsOnRight ) { rc = ResizeWindowRelative( CurrentWindow, EditVars.LineNumWinWidth, 0, 0, 0, FALSE ); if( rc != ERR_NO_ERR ) { return( rc ); } wi.x1 = x1; wi.x2 = x1 + EditVars.LineNumWinWidth - 1 - j; } else { rc = ResizeWindowRelative( CurrentWindow, 0, 0, -EditVars.LineNumWinWidth, 0, FALSE ); if( rc != ERR_NO_ERR ) { return( rc ); } wi.x1 = x2 - EditVars.LineNumWinWidth + 1; wi.x2 = x2 - j; } rc = NewWindow2( &CurrNumWindow, &wi ); if( rc != ERR_NO_ERR ) { return( rc ); } } else { CloseAWindow( CurrNumWindow ); rc = ResizeWindow( CurrentWindow, editw_info.x1, editw_info.y1, editw_info.x2, editw_info.y2, FALSE ); if( rc != ERR_NO_ERR ) { return( rc ); } } SetWindowCursor(); DCDisplayAllLines(); return( ERR_NO_ERR ); } /* LineNumbersSetup */
/* * tempMatch - show a temporary match */ static void tempMatch( i_mark *pos ) { SaveCurrentFilePos(); GoToLineNoRelCurs( pos->line ); GoToColumnOK( pos->column ); #ifdef __WIN__ DCDisplayAllLines(); DCUpdate(); SetWindowCursorForReal(); MyDelay( 150 ); RestoreCurrentFilePos(); DCDisplayAllLines(); DCUpdate(); #else MyDelay( 150 ); RestoreCurrentFilePos(); DCDisplayAllLines(); #endif } /* tempMatch */
/* * DisplayWorkLine - display the working copy of the current line */ void DisplayWorkLine( bool killsFlags ) { int i; if( killsFlags ) { DCDisplayAllLines(); } else { // could speed up a little by calling directly i = (int)( CurrentPos.line - LeftTopPos.line ); DCDisplaySomeLines( i, i ); } } /* DisplayWorkLine */
/* * SelectItem - select item to set from a menu */ vi_rc SelectItem( selectitem *si ) { int j; file *cfile; selflinedata sfd; vi_rc rc; tempFileSetup( &cfile, si->list, si->maxlist, 0, false ); /* * get selected line */ memset( &sfd, 0, sizeof( sfd ) ); sfd.f = cfile; sfd.wi= si->wi; sfd.title = si->title; sfd.allow_rl = si->allowrl; sfd.hilite = si->hilite; sfd.show_lineno = si->show_lineno; sfd.retevents = si->retevents; sfd.event = si->event; sfd.cln = si->cln; sfd.eiw = si->eiw; sfd.is_menu = si->is_menu; rc = SelectLineInFile( &sfd ); si->event = sfd.event; if( rc == ERR_NO_ERR ) { if( sfd.sl == -1 || sfd.sl == 0 ) { if( si->result != NULL ) { si->result[0] = 0; } si->num = -1; } else { j = (int) sfd.sl - 1; if( si->result != NULL ) { strcpy( si->result, si->list[j] ); } si->num = j; } } /* * done, free memory */ if( cfile != NULL ) { FreeEntireFile( cfile ); } DCDisplayAllLines(); return( rc ); } /* SelectItem */
/* * UnselectRegion - remove selected region */ void UnselectRegion( void ) { if( !SelRgn.selected ) { return; } SelRgn.selected = FALSE; EditFlags.Dragging = FALSE; #ifdef __WIN__ updateRegion(); #else markRegion( FALSE ); DCDisplayAllLines(); #endif } /* UnselectRegion */
/* * SelectItemAndValue - select item from list and give it a value */ vi_rc SelectItemAndValue( window_info *wi, char *title, char **list, int maxlist, vi_rc (*updatertn)( char *, char *, int * ), int indent, char **vals, int valoff ) { // int j; file *cfile; selflinedata sfd; vi_rc rc; tempFileSetup( &cfile, list, maxlist, indent, true ); for( ;; ) { /* * go get selected line */ memset( &sfd, 0, sizeof( sfd ) ); sfd.f = cfile; sfd.wi = wi; sfd.title = title; sfd.checkres = updatertn; sfd.cln = 1; sfd.eiw = NO_WINDOW; sfd.vals = vals; sfd.valoff = valoff; rc = SelectLineInFile( &sfd ); if( rc != ERR_NO_ERR ) { break; } if( sfd.sl == -1 ) { break; } // j = (int) sfd.sl - 1; } /* * done, free memory */ if( cfile != NULL ) { FreeEntireFile( cfile ); } DCDisplayAllLines(); return( rc ); } /* SelectItemAndValue */
/* * OpenWindowOnFile - open a window on a file */ vi_rc OpenWindowOnFile( const char *data ) { vi_rc rc; window_id wid; data = SkipLeadingSpaces( data ); if( data[0] == '\0' ) { data = NULL; } wid = current_window_id; rc = NewFile( data, true ); if( rc == ERR_NO_ERR ) { InactiveWindow( wid ); DCDisplayAllLines(); } return( rc ); } /* OpenWindowOnFile */
/* * MovePosition - move to a screen position */ vi_rc MovePosition( void ) { linenum lne, lines; vi_key key; vi_rc rc; if( RepeatDigits == 0 ) { lne = CurrentPos.line; } else { lne = GetRepeatCount(); if( IsPastLastLine( lne ) ) { return( ERR_INVALID_REDRAW ); } } lines = WindowAuxInfo( CurrentWindow, WIND_INFO_TEXT_LINES ); key = GetNextEvent( false ); switch( key ) { case '.': LeftTopPos.line = lne - lines / 2; break; case VI_KEY( ENTER ): LeftTopPos.line = lne; break; case '-': LeftTopPos.line = lne - lines + 1; break; default: return( ERR_INVALID_REDRAW ); } if( LeftTopPos.line < 1 ) { LeftTopPos.line = 1; } SetCurrentLineNumber( lne ); rc = CGimmeLinePtr( CurrentPos.line, &CurrentFcb, &CurrentLine ); CurrentPos.column = 1; DCInvalidateAllLines(); DCDisplayAllLines(); if( rc == ERR_NO_ERR ) { rc = GoToColumnOnCurrentLine( FindStartOfCurrentLine() ); } return( rc ); } /* MovePosition */
/* * OpenWindowOnFile - open a window on a file */ vi_rc OpenWindowOnFile( char *data ) { char *name; vi_rc rc; window_id wn; RemoveLeadingSpaces( data ); name = data; if( data[0] == 0 ) { name = NULL; } wn = CurrentWindow; rc = NewFile( name, true ); if( rc == ERR_NO_ERR ) { InactiveWindow( wn ); DCDisplayAllLines(); } return( rc ); } /* OpenWindowOnFile */
/* * updateRegion - update marked region */ static void updateRegion( void ) { #ifdef __WIN__ if( !SelRgn.selected ) { int min_line, max_line; min_line = last_start_line; if( min_line > last_start_line ) min_line = last_start_line; max_line = last_start_line; if( max_line < last_end_line ) max_line = last_end_line; DCDisplaySomeLines( min_line - LeftTopPos.line, max_line - LeftTopPos.line ); last_start_line = last_end_line = 1; return; } else { int min_start, max_start; int min_end, max_end; min_start = SelRgn.start.line; if( min_start > last_start_line ) min_start = last_start_line; max_start = SelRgn.start.line; if( max_start < last_start_line ) max_start = last_start_line; min_end = SelRgn.end.line; if( min_end > last_end_line ) min_end = last_end_line; max_end = SelRgn.end.line; if( max_end < last_end_line ) max_end = last_end_line; DCDisplaySomeLines( min_start - LeftTopPos.line, max_start - LeftTopPos.line ); DCDisplaySomeLines( min_end - LeftTopPos.line, max_end - LeftTopPos.line ); } last_start_line = SelRgn.start.line; last_end_line = SelRgn.end.line; #else markRegion( true ); DCDisplayAllLines(); #endif } /* updateRegion */
/* * ResizeWindow - give a window a new size */ vi_rc ResizeWindow( window_id wid, windim x1, windim y1, windim x2, windim y2, bool scrflag ) { window *oldw; // int bt, k; // char *txt, *tptr; // char *ot; // int i, j; oldw = WINDOW_FROM_ID( wid ); AccessWindow( oldw ); if( !ValidDimension( x1, y1, x2, y2, oldw->has_border ) ) { ReleaseWindow( oldw ); return( ERR_WIND_INVALID ); } RestoreOverlap( wid, scrflag ); AllocWindow( wid, x1, y1, x2, y2, oldw->has_border, oldw->has_gadgets, true, oldw->border_color1, oldw->border_color2, oldw->text_color, oldw->background_color ); MarkOverlap( wid ); /* * display the new text */ ClearWindow( wid ); if( oldw->title != NULL ) { WindowTitle( wid, oldw->title ); } else { DrawBorder( wid ); } DCResize( CurrentInfo ); DCDisplayAllLines(); DCUpdate(); FreeWindow( oldw ); ReleaseWindow( WINDOW_FROM_ID( wid ) ); return( ERR_NO_ERR ); } /* ResizeWindow */
/* * updateRegion - update marked region */ static void updateRegion( void ) { #ifdef __WIN__ if( SelRgn.selected == FALSE ) { DCDisplaySomeLines( __min( last_start_line, last_end_line ) - LeftTopPos.line, __max( last_start_line, last_end_line ) - LeftTopPos.line ); last_start_line = last_end_line = 1; return; } else { DCDisplaySomeLines( __min( SelRgn.start.line, last_start_line ) - LeftTopPos.line, __max( SelRgn.start.line, last_start_line ) - LeftTopPos.line ); DCDisplaySomeLines( __min( SelRgn.end.line, last_end_line ) - LeftTopPos.line, __max( SelRgn.end.line, last_end_line ) - LeftTopPos.line ); } last_start_line = SelRgn.start.line; last_end_line = SelRgn.end.line; #else markRegion( TRUE ); DCDisplayAllLines(); #endif } /* updateRegion */
/* * MoveScreenDown - expose bottom line */ vi_rc MoveScreenDown( void ) { linenum lne, cnt, lines, top, x; CFindLastLine( &lne ); lines = WindowAuxInfo( CurrentWindow, WIND_INFO_TEXT_LINES ); x = lne - lines + 1; if( x <= 0 ) { return( ERR_NO_ERR ); } cnt = GetRepeatCount(); top = LeftTopPos.line + cnt; if( top > x ) { top = x; } LeftTopPos.line = top; if( top >= CurrentPos.line ) { GoToLineNoRelCurs( top ); } DCDisplayAllLines(); SetWindowCursor(); return( ERR_NO_ERR ); } /* MoveScreenDown */
/* * MoveScreenUp - expose top line */ vi_rc MoveScreenUp( void ) { linenum lne, cnt, lines, top, nlne; lines = WindowAuxInfo( CurrentWindow, WIND_INFO_TEXT_LINES ); CFindLastLine( &lne ); cnt = GetRepeatCount(); top = LeftTopPos.line - cnt; if( top < 1 ) { top = 1; } LeftTopPos.line = top; if( CurrentPos.line >= top + lines ) { nlne = top + lines - 1; if( nlne > lne ) { nlne = lne; } GoToLineNoRelCurs( nlne ); } DCDisplayAllLines(); SetWindowCursor(); return( ERR_NO_ERR ); } /* MoveScreenUp */
/* * InsertTextForSpecialKey - insert text for ^O, ALT_O */ void InsertTextForSpecialKey( vi_key event, char *buff ) { linenum line; int type; if( CurrentFile == NULL ) { return; } line = CurrentPos.line; type = INSERT_BEFORE; if( event == VI_KEY( CTRL_O ) ) { type = INSERT_AFTER; line += 1; } Modified( true ); StartUndoGroup( UndoStack ); UndoInsert( line, line, UndoStack ); AddNewLineAroundCurrent( buff, strlen( buff ), type ); EndUndoGroup( UndoStack ); DCDisplayAllLines(); DCUpdate(); } /* InsertTextForSpecialKey */
/* * GoToColumn - go to a specified column */ vi_rc GoToColumn( int colno, int maxcol ) { int vc; if( CurrentFile == NULL ) { return( ERR_NO_FILE ); } if( maxcol == 0 ) { maxcol = 1; } if( colno == 0 ) { colno = 1; } if( colno < 1 || colno > maxcol ) { return( ERR_NO_SUCH_COLUMN ); } /* * compute new location, and re-display text if needed */ VirtualColumnDesired = VirtualColumnOnCurrentLine( colno ); CurrentPos.column = colno; if( !CheckLeftColumn() ) { DCDisplayAllLines(); PositionHorizontalScrollThumb( current_window_id, LeftTopPos.column ); } SetWindowCursor(); vc = VirtualColumnOnCurrentLine( CurrentPos.column ); UpdateStatusWindow(); VarAddGlobalLong( "C", (long) vc ); UpdateCursorDrag(); return( ERR_NO_ERR ); } /* GoToColumn */
/* * EditFile - read a file into text */ vi_rc EditFile( char *name, bool dammit ) { char *fn, **list, *currfn; int i, cnt, ocnt; int j, len; window_id wn = NO_WINDOW; char cdir[FILENAME_MAX]; info *ci, *il; bool usedir = false; char mask[FILENAME_MAX]; bool reset_dir; int index; char *altname = NULL; vi_rc rc; fn = MemAlloc( FILENAME_MAX ); /* * get file name */ strcpy( cdir, CurrentDirectory ); reset_dir = false; RemoveLeadingSpaces( name ); if( name[0] == '$' ) { EliminateFirstN( name, 1 ); usedir = true; } fn[0] = 0; // if( NextWord1( name, fn ) <= 0 ) if( GetStringWithPossibleQuote2( name, fn, false ) != ERR_NO_ERR ) { usedir = true; mask[0] = '*'; mask[1] = 0; } if( usedir ) { if( EditFlags.ExMode ) { MemFree( fn ); return( ERR_INVALID_IN_EX_MODE ); } len = strlen( fn ); if( len > 0 ) { i = len - 1; strcpy( mask, fn ); cnt = 0; while( i >= 0 ) { if( fn[i] == FILE_SEP ) { for( j = i + 1; j <= len; j++ ) { mask[j - (i + 1)] = fn[j]; } cnt = i; break; } i--; } fn[cnt] = 0; } if( fn[0] != 0 ) { rc = SelectFileOpen( fn, &fn, mask, true ); } else { #ifdef __WIN__ if( name[0] == '\0' ) { altname = MemAlloc( 1000 ); rc = SelectFileOpen( CurrentDirectory, &altname, mask, true ); NextWord1( altname, fn ); // if multiple, kill path if( isMultipleFiles( altname ) ) { NextWord1( altname, fn ); // get 1st name } } else { rc = SelectFileOpen( CurrentDirectory, &fn, mask, true ); } #else rc = SelectFileOpen( CurrentDirectory, &fn, mask, true ); #endif } if( altname ) { name = altname; } if( rc != ERR_NO_ERR || fn[0] == 0 ) { MemFree( fn ); SetCWD( cdir ); return( rc ); } } /* * loop through all files */ rc = ERR_NO_ERR; EditFlags.WatchForBreak = true; #ifdef __WIN__ ToggleHourglass( true ); #endif do { if( IsDirectory( fn ) ) { if( EditFlags.ExMode ) { rc = ERR_INVALID_IN_EX_MODE; reset_dir = true; break; } rc = SelectFileOpen( fn, &fn, "*", false ); if( rc != ERR_NO_ERR ) { reset_dir = true; break; } if( fn[0] == 0 ) { reset_dir = true; rc = ERR_NO_ERR; break; } } currfn = fn; ocnt = cnt = ExpandFileNames( currfn, &list ); if( !cnt ) { cnt = 1; } else { currfn = list[0]; } /* * loop through all expanded files */ index = 1; while( cnt > 0 ) { cnt--; /* * quit current file if ! specified, else just save current state */ if( dammit ) { ci = InfoHead; if( CurrentInfo == ci ) { ci = ci->next; } RemoveFromAutoSaveList(); #ifdef __WIN__ CloseAChildWindow( CurrentWindow ); #else CloseAWindow( CurrentWindow ); #endif FreeUndoStacks(); FreeMarkList(); FreeEntireFile( CurrentFile ); MemFree( DeleteLLItem( (ss **)&InfoHead, (ss **)&InfoTail, (ss *)CurrentInfo ) ); CurrentInfo = NULL; CurrentWindow = NO_WINDOW; } else { ci = CurrentInfo; SaveCurrentInfo(); wn = CurrentWindow; } /* * see if new file is already being edited */ SaveCurrentInfo(); for( il = InfoHead; il != NULL; il = il->next ) { if( SameFile( il->CurrentFile->name, currfn ) ) { break; } if( strcmp( CurrentDirectory, il->CurrentFile->home ) ) { /* directory has changed -- check with full path * note that this will fail if an absolute path * was specified thus we do the regular check first */ char path[FILENAME_MAX]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath( il->CurrentFile->name, drive, dir, fname, ext ); if( !strlen( drive ) ) { _splitpath( il->CurrentFile->home, drive, NULL, NULL, NULL ); } if( !strlen( dir ) ) { _splitpath( il->CurrentFile->home, NULL, dir, NULL, NULL ); } else if( dir[0] != '\\' ) { char dir2[_MAX_DIR]; _splitpath( il->CurrentFile->home, NULL, dir2, NULL, NULL ); strcat( dir2, dir ); strcpy( dir, dir2 ); } _makepath( path, drive, dir, fname, ext ); if( SameFile( path, currfn ) ) { break; } } } if( il != NULL ) { BringUpFile( il, true ); } else { /* * file not edited, go get it */ rc = NewFile( currfn, false ); if( rc != ERR_NO_ERR && rc != NEW_FILE ) { RestoreInfo( ci ); DCDisplayAllLines(); break; } if( !dammit ) { InactiveWindow( wn ); } if( EditFlags.BreakPressed ) { break; } } if( cnt > 0 ) { currfn = list[index]; index++; } } if( ocnt > 0 ) { MemFreeList( ocnt, list ); } if( EditFlags.BreakPressed ) { ClearBreak(); break; } } while( NextWord1( name, fn ) > 0 ); if( altname ) { MemFree( altname ); } MemFree( fn ); #ifdef __WIN__ ToggleHourglass( false ); #endif EditFlags.WatchForBreak = false; if( reset_dir ) { SetCWD( cdir ); } return( rc ); } /* EditFile */
/* * IMEnter - process the enter key in insert mode */ vi_rc IMEnter( void ) { char *buff, *buffx; int len, col, el; if( CurrentFile == NULL ) { return( ERR_NO_FILE ); } CurrentFile->need_autosave = TRUE; startNewLineUndo(); CheckAbbrev( abbrevBuff, &abbrevCnt ); abbrevCnt = 0; /* * truncate the working line */ buff = StaticAlloc(); buffx = StaticAlloc(); el = WorkLine->len - CurrentPos.column + 1; if( el > 0 && WorkLine->len > 0 ) { memcpy( buff, &WorkLine->data[CurrentPos.column - 1], el + 1 ); WorkLine->len -= el; WorkLine->data[CurrentPos.column - 1] = 0; } else { el = 0; buff[0] = 0; } len = trimWorkLine(); /* * replace the current line with the working copy */ ReplaceCurrentLine(); if( currLineRepUndo ) { CurrentLineReplaceUndoEnd( FALSE ); currLineRepUndo = FALSE; } /* * create a new line, insert leading spaces if needed * and copy in the truncation */ if( EditFlags.AutoIndent ) { len = GetAutoIndentAmount( buffx, len, FALSE ); el += len; strcat( buffx, buff ); AddNewLineAroundCurrent( buffx, el, INSERT_AFTER ); col = len + 1; } else { AddNewLineAroundCurrent( buff, el, INSERT_AFTER ); col = 1; } UndoInsert( CurrentPos.line + 1, CurrentPos.line + 1, UndoStack ); /* * display the result */ DCDisplayAllLines(); GoToLineRelCurs( CurrentPos.line + 1 ); GoToColumnOK( col ); GetCurrentLine(); StaticFree( buff ); StaticFree( buffx ); return( ERR_NO_ERR ); } /* IMEnter */
/* * GenericJoinCurrentLineToNext */ vi_rc GenericJoinCurrentLineToNext( bool remsp ) { line *nline = CurrentLine; fcb *nfcb = CurrentFcb; int i, j, k; vi_rc rc; /* * get next line data */ rc = CGimmeNextLinePtr( &nfcb, &nline ); if( rc != ERR_NO_ERR ) { return( rc ); } if( CurrentLine->len + nline->len + 1 >= EditVars.MaxLine ) { return( ERR_LINE_FULL ); } /* * now, copy in the data */ StartUndoGroup( UndoStack ); CurrentLineReplaceUndoStart(); CurrentLineReplaceUndoEnd( true ); GetCurrentLine(); if( remsp ) { while( WorkLine->len > 0 && WorkLine->data[WorkLine->len - 1] == ' ' ) { WorkLine->data[WorkLine->len - 1] = 0; WorkLine->len--; } j = FindStartOfALine( nline ) - 1; k = 0; if( !(j == 0 && nline->data[0] == ' ') ) { if( WorkLine->len != 0 ) { WorkLine->data[WorkLine->len] = ' '; k = WorkLine->len + 1; } for( i = j; i <= nline->len; i++ ) { WorkLine->data[k + i - j] = nline->data[i]; } } } else { k = WorkLine->len; for( i = 0; i <= nline->len; i++ ) { WorkLine->data[k + i] = nline->data[i]; } } WorkLine->len = strlen( WorkLine->data ); ReplaceCurrentLine(); /* * delete next line */ rc = DeleteLineRange( CurrentPos.line + 1, CurrentPos.line + 1, 0 ); if( rc != ERR_NO_ERR ) { return( rc ); } EndUndoGroup( UndoStack ); if( remsp ) { if( k < 2 ) { k = 2; } rc = GoToColumn( k - 1, CurrentLine->len ); if( rc != ERR_NO_ERR ) { return( rc ); } } DCDisplayAllLines(); return( ERR_NO_ERR ); } /* GenericJoinCurrentLineToNext */
/* * ResizeWindow - give a window a new size */ vi_rc ResizeWindow( window_id wn, int x1, int y1, int x2, int y2, int scrflag ) { wind *tmp, *w; int bt, k; // char *txt, *tptr; // char *ot; // int i, j; w = AccessWindow( wn ); if( !ValidDimension( x1, y1, x2, y2, w->has_border ) ) { ReleaseWindow( w ); return( ERR_WIND_INVALID ); } tmp = AllocWindow( x1, y1, x2, y2, w->has_border, w->border_color1, w->border_color2, w->text_color, w->background_color ); tmp->id = wn; tmp->has_gadgets = w->has_gadgets; // txt = MemAlloc( w->width + 1 ); // tptr = txt; RestoreOverlap( wn, scrflag ); Windows[wn] = tmp; tmp->accessed = TRUE; ResetOverlap( tmp ); MarkOverlap( wn ); /* * display the new text */ k = 1; bt = (int) w->has_border; ClearWindow( wn ); if( w->title != NULL ) { WindowTitle( wn, w->title ); } else { DrawBorder( wn ); } #if 0 for( j = bt; j < w->height - bt; j++ ) { ot = &(w->text[(j * w->width) * sizeof( char_info )]); for( i = bt; i < w->width - bt; i++ ) { *txt++ = ot[i * sizeof( char_info )]; } *txt = 0; DisplayLineInWindow( wn, k++, tptr ); txt = tptr; } #else DCResize( CurrentInfo ); DCDisplayAllLines(); DCUpdate(); #endif FreeWindow( w ); ReleaseWindow( tmp ); return( ERR_NO_ERR ); } /* ResizeWindow */
/* * SelectFileOpen - select file from specified directory */ vi_rc SelectFileOpen( char *dir, char **result_ptr, char *mask, bool want_all_dirs ) { char dd[FILENAME_MAX], cdir[FILENAME_MAX]; int j; file *cfile; fcb *cfcb; line *cline; selflinedata sfd; bool need_entire_path; char *result = *result_ptr; vi_rc rc; /* * get current directory */ strcpy( dd, dir ); strcpy( cdir, dir ); SetCWD( dir ); need_entire_path = FALSE; /* * work through all files */ for( ;; ) { if( dd[strlen( dd ) - 1] != FILE_SEP ) { strcat( dd, FILE_SEP_STR ); } strcat( dd, mask ); rc = GetSortDir( dd, want_all_dirs ); if( rc != ERR_NO_ERR ) { return( rc ); } /* * allocate temporary file structure */ cfile = FileAlloc( NULL ); FormatDirToFile( cfile, TRUE ); /* * go get selected line */ memset( &sfd, 0, sizeof( sfd ) ); sfd.f = cfile; sfd.wi = &dirw_info; sfd.title = CurrentDirectory; sfd.show_lineno = TRUE; sfd.cln = 1; sfd.eiw = NO_WINDOW; rc = SelectLineInFile( &sfd ); if( rc != ERR_NO_ERR ) { break; } if( sfd.sl == -1 ) { result[0] = 0; break; } j = (int) sfd.sl - 1; if( j >= DirFileCount || DirFiles[j]->attr & _A_SUBDIR ) { if( j >= DirFileCount ) { GimmeLinePtr( j + 1, cfile, &cfcb, &cline ); dd[0] = cline->data[3]; dd[1] = ':'; dd[2] = 0; } else { strcpy( dd, cdir ); if( dd[strlen(dd) - 1] != FILE_SEP ) { strcat( dd, FILE_SEP_STR ); } strcat( dd, DirFiles[j]->name ); } FreeEntireFile( cfile ); rc = SetCWD( dd ); if( rc != ERR_NO_ERR ) { return( rc ); } need_entire_path = TRUE; strcpy( cdir, CurrentDirectory ); strcpy( dd, CurrentDirectory ); continue; } if( need_entire_path ) { strcpy( result, CurrentDirectory ); if( result[strlen(result) - 1] != FILE_SEP ) { strcat( result, FILE_SEP_STR ); } } else { result[0] = 0; } strcat( result, DirFiles[j]->name ); break; } /* * done, free memory */ FreeEntireFile( cfile ); DCDisplayAllLines(); return( rc ); } /* SelectFileOpen */
/* * goToLine - go to a specified line number */ static vi_rc goToLine( linenum lineno, bool relcurs ) { int text_lines, tl; linenum diff, cwl, nwl; // linenum s, e, hiddcnt; bool dispall, pageshift; fcb *cfcb; line *cline; int pad; vi_rc rc; if( lineno < 1 ) { return( ERR_NO_SUCH_LINE ); } /* * get pointer to requested line */ rc = CGimmeLinePtr( lineno, &cfcb, &cline ); if( rc != ERR_NO_ERR ) { return( rc ); } #if 0 if( cline->u.ld.hidden ) { GetHiddenRange( lineno, &s, &e ); if( lineno > CurrentPos.line ) { lineno = e + 1; } else { lineno = s - 1; } rc = CGimmeLinePtr( lineno, &cfcb, &cline ); if( rc != ERR_NO_ERR ) { return( rc ); } } #endif /* * compute new location */ CurrentFcb = cfcb; CurrentLine = cline; diff = lineno - CurrentPos.line; if( diff == 0 && !EditFlags.GlobalInProgress ) { return( ERR_NO_ERR ); } cwl = CurrentPos.line - LeftTopPos.line + 1; nwl = cwl + diff; /* * if we go off the window, relocate */ pageshift = false; dispall = false; text_lines = WindowAuxInfo( current_window_id, WIND_INFO_TEXT_LINES ); if( nwl < 1 || nwl > text_lines ) { tl = text_lines / 2; if( !relcurs ) { LeftTopPos.line = lineno - tl; } else { LeftTopPos.line = lineno + 1 - cwl; pad = ( EditFlags.JumpyScroll ) ? 1 : 0; if( diff > 0 ) { LeftTopPos.line += pad; diff += pad; } else { LeftTopPos.line -= pad; diff -= pad; } if( diff > -tl && diff < tl && !dispall ) { pageshift = true; } } if( LeftTopPos.line < 1 ) { assert( diff <= 0 ); // < -> <= W.Briscoe 20031003 to avoid debug build failure of // C:\watcom\source\docs\nt) wmake -h -f ..\mif\master.mif hbook=wccerrs dotarget=nt diff += ( 1 - LeftTopPos.line ); LeftTopPos.line = 1; } if( LeftTopPos.line > lineno ) { assert( diff > 0 ); diff = LeftTopPos.line - lineno; LeftTopPos.line = lineno; } dispall = true; } #if 0 hiddcnt = GetHiddenLineCount( LeftTopPos.line, lineno ); if( hiddcnt > 0 ) { pageshift = false; dispall = true; } #endif if( CheckCurrentColumn() || EditFlags.Dragging ) { // pageshift wont help if we also have to column shift // and not really useful if dragging dispall = true; pageshift = false; } /* call SetCurrentLineNumber AFTER LeftTopPos.line set & CurrentColumn checked */ SetCurrentLineNumber( lineno ); if( pageshift ) { dispall = false; ShiftWindowUpDown( current_window_id, diff ); if( EditFlags.LineNumbers ) { ShiftWindowUpDown( curr_num_window_id, diff ); } if( diff > 0 ) { DCDisplaySomeLines( text_lines - diff, text_lines - 1 ); } else { DCDisplaySomeLines( 0, -diff - 1 ); } } UpdateStatusWindow(); SetWindowCursor(); if( dispall ) { DCInvalidateAllLines(); // lines definitely invalid DCDisplayAllLines(); } return( ERR_NO_ERR ); } /* goToLine */