@@ -973,21 +973,43 @@ private void CmdHistoryPrev()
973973 SetText ( this . _history . Previous ( ) ) ;
974974 }
975975
976- // Edit the current cmdlet in Vim
977- // BUGBUGBUG - this shouldn't be hardcoded to vim or '.tl'.
976+ // Edits the current command line in the program specified by the EDITOR environment
977+ // variable. If EDITOR is not set, then it defaults to 'notepad' on Windows and 'nano'
978+ // on UNIX systems.
978979 private void CmdVisualEdit ( )
979980 {
980- string tempFile = System . IO . Path . GetTempFileName ( ) + ".tl" ;
981- System . IO . File . WriteAllText ( tempFile , this . _text . ToString ( ) ) ;
981+ string editor = Environment . GetEnvironmentVariable ( "EDITOR" ) ;
982+ if ( editor == null || editor . Length == 0 )
983+ {
984+ Console . WriteLine ( "\n *** 'EDITOR' environment variable is not set, using default editor." ) ;
985+ #if UNIX
986+ editor = "nano" ;
987+ #else
988+ string path = Path . Combine ( Environment . GetEnvironmentVariable ( "SystemRoot" ) , "System32" ) ;
989+ editor = Path . Combine ( path , "notepad.exe" ) ;
990+ #endif
991+ }
992+
993+ if ( File . Exists ( editor ) == false )
994+ {
995+ Console . WriteLine ( $ "\n *** Unable to find editor program '{ editor } '; giving up.\n ") ;
996+ CmdDone ( ) ;
997+ return ;
998+ }
999+
1000+ string tempFile = Path . GetTempFileName ( ) + ".tl" ; // Add '.tl' so we get syntax highlighting if available.
1001+ File . WriteAllText ( tempFile , this . _text . ToString ( ) ) ;
1002+
9821003 var process = new System . Diagnostics . Process ( ) ;
983- process . StartInfo . FileName = "vim" ;
1004+ process . StartInfo . FileName = editor ;
9841005 process . StartInfo . WorkingDirectory = Environment . CurrentDirectory ;
985- process . StartInfo . Arguments = " -n " + tempFile ;
986- // process.StartInfo.CreateNoWindow = true;
1006+ process . StartInfo . Arguments = tempFile ;
1007+ // process.StartInfo.CreateNoWindow = true; // Setting this causes the editor to hang on startup
9871008 process . StartInfo . UseShellExecute = false ;
1009+ Console . WriteLine ( $ "*** Starting editor '{ editor } ' on file '{ tempFile } '\n ") ;
9881010 process . Start ( ) ;
9891011 process . WaitForExit ( ) ;
990- string newText = System . IO . File . ReadAllText ( tempFile ) . Trim ( '\n ' ) ;
1012+ string newText = System . IO . File . ReadAllText ( tempFile ) . Trim ( '\n ' ) . Trim ( ' \r ' ) ;
9911013 System . IO . File . Delete ( tempFile ) ;
9921014 SetText ( newText ) ;
9931015 CmdDone ( ) ;
0 commit comments