/* * 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 */
/* * WindowTitleAOI - set the title of a window, active or inactive */ void WindowTitleAOI( window_id wn, char *title, bool active ) { wind *w; w = Windows[wn]; MemFree( w->title ); if( title == NULL ) { w->title = NULL; } else { AddString( &(w->title), title ); } if( active ) { DrawBorder( wn ); } else { InactiveWindow( wn ); } } /* WindowTitleAOI */
/* * 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 */
/* * 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 */
/* * MainWindowProc - procedure for main (root) window */ WINEXPORT LRESULT CALLBACK MainWindowProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) { RECT rect; vi_rc rc; HANDLE hfileinfo; int cnt, i; char *buff; switch( msg ) { case WM_CREATE: Root = hwnd; GetClientRect( hwnd, &rect ); EditContainer = CreateContainerWindow( &rect ); InitWindows(); DragAcceptFiles( hwnd, TRUE ); timerID = SetTimer( hwnd, TIMER_ID, 60L * 1000L, NULL ); break; case WM_DROPFILES: hfileinfo = (HANDLE) wparam; cnt = DragQueryFile( hfileinfo, (UINT)-1, NULL, 0 ); buff = alloca( FILENAME_MAX + 2 ); /* we add a " at the beginning and at the end so we can handle path- and filenames with spaces */ if( buff != NULL ) { buff[0] = '"'; /* one " at the beginning of the filename */ for( i = 0; i < cnt; i++ ) { if( DragQueryFile( hfileinfo, i, buff + 1, FILENAME_MAX ) == (UINT)-1 ) { break; } strcat( buff, "\"" ); rc = EditFile( buff, FALSE ); if( rc > ERR_NO_ERR ) { Error( GetErrorMsg( rc ) ); } } } DragFinish( hfileinfo ); break; case WM_TIMER: UpdateStatusWindow(); break; case WM_KEYDOWN: if( WindowsKeyPush( wparam, HIWORD( lparam ) ) ) { return( 0 ); } break; case WM_SIZE: DefFrameProc( hwnd, EditContainer, msg, wparam, lparam ); RootState = wparam; if( wparam != SIZE_MINIMIZED ) { ResizeRoot(); GetWindowRect( hwnd, &RootRect ); if( wparam != SIZE_MAXIMIZED ) { RootState = 0; } } return( 0 ); case WM_MOVE: DefFrameProc( hwnd, EditContainer, msg, wparam, lparam ); if( RootState != SIZE_MINIMIZED ) { GetWindowRect( hwnd, &RootRect ); } return( 0 ); case WM_ACTIVATEAPP: if( BAD_ID( CurrentWindow ) ) { break; } SetFocus( Root ); #if 0 if( !wparam ) { InactiveWindow( CurrentWindow ); } else { SendMessage( EditContainer, WM_MDIACTIVATE, (WPARAM)CurrentWindow, 0L ); } #endif if( wparam ) { ResetEditWindowCursor( CurrentWindow ); } else { GoodbyeCursor( CurrentWindow ); } break; case WM_MOUSEACTIVATE: SetFocus( hwnd ); return( MA_ACTIVATE ); case WM_SETFOCUS: if( BAD_ID( CurrentWindow ) ) { break; } if( !IsIconic( CurrentWindow ) ) { SendMessage( EditContainer, WM_MDIACTIVATE, (WPARAM)CurrentWindow, 0L ); DCUpdate(); SetWindowCursor(); SetWindowCursorForReal(); return( 0 ); } break; case WM_NCLBUTTONDBLCLK: break; case WM_COMMAND: if( LOWORD( wparam ) > 0xF000 ) { break; } else { rc = MenuCommand( LOWORD( wparam ) ); if( rc != MENU_COMMAND_NOT_HANDLED ) { DCUpdateAll(); if( rc > ERR_NO_ERR ) { char *msg; msg = GetErrorMsg( rc ); Error( msg ); } } SetWindowCursor(); } return( 0 ); case WM_INITMENU: if( (HMENU)wparam == GetMenu( hwnd ) ) { HandleInitMenu( (HMENU)wparam ); } else { ResetMenuBits(); } break; case WM_MENUSELECT: HandleMenuSelect( wparam, lparam ); break; case WM_ENDSESSION: if( wparam ) { ExitEditor( 0 ); // will not return } return( 0 ); case WM_QUERYENDSESSION: return( ExitWithPrompt( FALSE, TRUE ) ); case WM_CLOSE: ExitWithPrompt( TRUE, TRUE ); return( 0 ); #ifdef __NT__ case WM_MOUSEWHEEL: { int i, increment; ULONG linesPerNotch; HWND activeWnd; activeWnd = (HWND)SendMessage( EditContainer, WM_MDIGETACTIVE, 0, 0 ); SystemParametersInfo( SPI_GETWHEELSCROLLLINES, 0, &linesPerNotch, 0 ); increment = GET_WHEEL_DELTA_WPARAM( wparam ) / 120; // see WM_MOUSEWHEEL-documentation for information about the "120" if( increment > 0 ) { for( i = 0; i < increment * (int)linesPerNotch; i++ ) { SendMessage( activeWnd, WM_VSCROLL, SB_LINEUP, 0 ); } } else { for( i = 0; i < (-increment) * (int)linesPerNotch; i++ ) { SendMessage( activeWnd, WM_VSCROLL, SB_LINEDOWN, 0 ); } } } return( 0 ); #endif case WM_DESTROY: DestroyToolBar(); DragAcceptFiles( hwnd, FALSE ); EditContainer = 0; if( timerID ) { KillTimer( hwnd, TIMER_ID ); } return( 0 ); } return( DefFrameProc( hwnd, EditContainer, msg, wparam, lparam ) ); } /* MainWindowProc */
/* * EditFile - read a file into text */ vi_rc EditFile( const char *name, bool dammit ) { char *fn, **list, *currfn; int i, cnt, ocnt; int j, len; window_id wid = NO_WINDOW; char cdir[FILENAME_MAX]; info *ci, *il; bool usedir = false; char mask[FILENAME_MAX]; bool reset_dir; int index; #ifdef __WIN__ char *altname = NULL; #endif vi_rc rc; fn = MemAlloc( FILENAME_MAX ); /* * get file name */ strcpy( cdir, CurrentDirectory ); reset_dir = false; name = SkipLeadingSpaces( name ); if( name[0] == '$' ) { ++name; usedir = true; } fn[0] = '\0'; // if( NextWord1FN( 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 ) { strcpy( mask, fn ); cnt = 0; for( i = len; i-- > 0; ) { if( fn[i] == FILE_SEP ) { for( j = i + 1; j <= len; j++ ) { mask[j - (i + 1)] = fn[j]; } cnt = i; break; } } 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 ); name = GetNextFileName( altname, fn ); // if multiple, kill path if( isMultipleFiles( name ) ) { name = GetNextFileName( name, fn ); // get 1st name } } else { rc = SelectFileOpen( CurrentDirectory, &fn, mask, true ); } #else rc = SelectFileOpen( CurrentDirectory, &fn, mask, true ); #endif } 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( current_window_id ); #else CloseAWindow( current_window_id ); #endif FreeUndoStacks(); FreeMarkList(); FreeEntireFile( CurrentFile ); MemFree( DeleteLLItem( (ss **)&InfoHead, (ss **)&InfoTail, (ss *)CurrentInfo ) ); CurrentInfo = NULL; current_window_id = NO_WINDOW; } else { ci = CurrentInfo; SaveCurrentInfo(); wid = current_window_id; } /* * 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]; size_t path_len; _splitpath( il->CurrentFile->name, drive, dir, fname, ext ); if( drive[0] == '\0' ) { _splitpath( il->CurrentFile->home, drive, NULL, NULL, NULL ); } strcpy( path, il->CurrentFile->home ); path_len = strlen( path ); if( path_len-- > 0 ) { #ifdef __UNIX__ if( path[path_len] != FILE_SEP ) { #else if( path[path_len] != DRV_SEP && path[path_len] != FILE_SEP ) { #endif strcat( path, FILE_SEP_STR ); } } if( dir[0] == '\0' ) { _splitpath( path, NULL, dir, NULL, NULL ); } else if( dir[0] != FILE_SEP ) { char dir2[_MAX_DIR]; _splitpath( path, 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( wid ); } if( EditFlags.BreakPressed ) { break; } } if( cnt > 0 ) { currfn = list[index]; index++; } } if( ocnt > 0 ) { MemFreeList( ocnt, list ); } if( EditFlags.BreakPressed ) { ClearBreak(); break; } name = GetNextFileName( name, fn ); } while( *fn != '\0' ); #ifdef __WIN__ if( altname != NULL ) { MemFree( altname ); } #endif MemFree( fn ); #ifdef __WIN__ ToggleHourglass( false ); #endif EditFlags.WatchForBreak = false; if( reset_dir ) { SetCWD( cdir ); } return( rc ); } /* EditFile */ #ifndef __WIN__ static const char *fileOpts[] = { (const char *)"<F1> Go To", (const char *)"<F2> Quit", (const char *)"<F3> Save & Quit" }; static const vi_key fileopts_evlist[] = { VI_KEY( F1 ), VI_KEY( F2 ), VI_KEY( F3 ), VI_KEY( DUMMY ) }; /* * EditFileFromList - edit from file in current active list */ vi_rc EditFileFromList( void ) { int i, tmp, j, n = 0, fcnt; window_id wid; bool repeat = true; info *cinfo; char **list, modchar; bool show_lineno; window_info wi; selectitem si; vi_rc rc; /* * set up options for file list */ memcpy( &wi, &extraw_info, sizeof( window_info ) ); wi.area.x1 = 2; wi.area.x2 = 19; rc = DisplayExtraInfo( &wi, &wid, fileOpts, sizeof( fileOpts ) / sizeof( fileOpts[0] ) ); if( rc != ERR_NO_ERR ) { return( rc ); } while( repeat > 0 ) { /* * set up for this pass */ repeat = false; MoveWindowToFrontDammit( wid, false ); SaveCurrentInfo(); /* * allocate a buffer for strings, add strings */ list = (char **) MemAlloc( GimmeFileCount() * sizeof( char * ) ); for( j = 0, cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next, ++j ) { list[j] = MemAlloc( strlen( cinfo->CurrentFile->name ) + 3 ); if( cinfo->CurrentFile->modified ) { modchar = '*'; } else { modchar = ' '; } MySprintf( list[j], "%c %s", modchar, cinfo->CurrentFile->name ); } fcnt = j; tmp = filelistw_info.area.y2; i = filelistw_info.area.y2 - filelistw_info.area.y1 + 1; if( filelistw_info.has_border ) { i -= 2; } if( j < i ) { filelistw_info.area.y2 -= ( i - j ); } show_lineno = true; /* * get file */ if( n + 1 > j ) { n = j - 1; } memset( &si, 0, sizeof( si ) ); si.wi = &filelistw_info; si.title = "Current Files"; si.list = list; si.maxlist = j; si.num = n; si.retevents = fileopts_evlist; si.event = VI_KEY( DUMMY ); si.show_lineno = show_lineno; si.cln = n + 1; si.eiw = wid; rc = SelectItem( &si ); n = si.num; if( rc == ERR_NO_ERR ) { if( n >= 0 ) { cinfo = InfoHead; for( j = 0; j < n; ++j ) { cinfo = cinfo->next; } BringUpFile( cinfo, true ); switch( si.event ) { case VI_KEY( DUMMY ): case VI_KEY( F1 ): break; case VI_KEY( F2 ): rc = NextFile(); if( rc <= ERR_NO_ERR ) { repeat = true; } break; case VI_KEY( F3 ): rc = SaveAndExit( NULL ); if( rc <= ERR_NO_ERR ) { repeat = true; } break; } } } filelistw_info.area.y2 = tmp; MemFreeList( fcnt, list ); } /* * get rid of option stuff */ CloseAWindow( wid ); return( rc ); } /* EditFileFromList */