Code: Select all
@if (@CodeSection == @Batch) @then
@echo off
rem Edlin.bat: Emulator of old MS-DOS edlin.com line-oriented text editor
rem Antonio Perez Ayala
rem Version 0.1 (preliminary): Replace, Search and Transfer commands not yet implemented
rem Things to do:
rem - Minor adjustment of final "current line" in a couple commands
rem - Implement relative, (.) and (#) line numbers
rem - More extensive error checking in commands
if "%~1" equ "" echo File name must be specified& goto :EOF
if "%~1" equ "/?" (
echo Starts Edlin, a line-oriented text editor.
echo/
echo EDLIN [drive:][path]filename
echo/
choice /M "Do you want to open the Edlin article in Wikipedia "
if not errorlevel 2 explorer http://en.wikipedia.org/wiki/Edlin
goto :EOF
)
rem Load the file, if it exists
setlocal DisableDelayedExpansion
set /A curLine=0,lastLine=0
if exist %1 (
for /F "delims=" %%a in ('findstr /N "^" %1') do (
set "line=%%a"
set /A lastLine+=1
setlocal EnableDelayedExpansion
for /F "tokens=1,* delims=ÿ" %%b in ("!lastLine!ÿ!line:*:=!") do endlocal & set "line[%%b]=%%c"
)
set curLine=1
set "newFile="
echo End of input file
) else (
set newFile=True
echo New file
)
setlocal EnableDelayedExpansion
set quote="
set digits=0123456789
:getCommand
if %curLine% gtr %lastLine% set curLine=%lastLine%
set "command="
set /P "command=*"
rem Separate command parameters
set "p1=" & set "p2=" & set "p3=" & set "p4="
if not defined command (
set "letter=#"
goto execCommand
)
set "letter=!command:~-1!"
if "!digits:%letter%=!" neq "%digits%" (
set "letter=#"
set p1=%command%
goto execCommand
)
set "command=!command:~0,-1!"
if defined command (
if "!command:~0,1!" equ "," set "command=!quote!!command!"
for /F "tokens=1-4 delims=," %%a in ("!command:,,=,"",!") do (
set "p1=%%~a" & set "p2=%%~b" & set "p3=%%~c" & set "p4=%%~d"
)
)
:execCommand
call :command-%letter% %1
if /I "%letter%" equ "e" goto :EOF
if /I "%letter%" equ "q" goto :EOF
goto getCommand
Copy lines from Begin to End and insert they at Dest, [repeat Times times]
:command-C [begin],[end],dest[,times]Copy
if defined p1 (set begin=%p1%) else set begin=%curLine%
if defined p2 (set end=%p2%) else set end=%curLine%
if %end% gtr %lastLine% set end=%lastLine%
set dest=%p3%
if %dest% gtr %lastLine% set /A dest=lastLine+1
if defined p4 (set times=%p4%) else set times=1
rem Renumber lines from Dest to lastLine, if any
set /A lines=end-begin+1
if %dest% leq %lastLine% (
set /A newDest=dest+lines*times
call :copyLines %dest% %lastLine% !newDest!
if %dest% lss %begin% set /A begin+=lines*times, end+=lines*times
) else (
set /A dest=lastLine+1
)
set /A curLine=dest, lastLine+=lines*times
rem Copy lines
for /L %%i in (1,1,%times%) do (
call :copyLines %begin% %end% !dest!
set /A dest+=lines
)
exit /B
Delete lines from Begin to End
:command-D [begin][,end]Delete
if defined p1 (set begin=%p1%) else set begin=%curLine%
if defined p2 (set end=%p2%) else set end=%begin%
if %end% gtr %lastLine% set end=%lastLine%
set /A newBegin=end+1
if %newBegin% leq %lastLine% call :copyLines %newBegin% %lastLine% %begin%
set /A curLine=begin, lastLine-=end-begin+1
exit /B
edit Line
:command-# [line]
if defined p1 (set "begin=%p1%") else set /A begin=curLine+1
if defined line[%begin%] (
CScript //nologo //E:JScript "%~F0" "!line[%begin%]!{ENTER}"
set "num= %begin%"
set /P "=<!num:~-7!:*"
set /P "line[%begin%]=>!num:~-7!:*"
set curLine=%begin%
)
exit /B
End/Save
:command-E End
if not defined newFile (
del "%~DPN1.bak" 2> NUL
ren "%~1" "%~N1.bak"
)
(for /L %%i in (1,1,%lastLine%) do echo(!line[%%i]!) > "%~1"
exit /B
Insert new text at Line
:command-I [line]Insert
if defined p1 (set begin=%p1%) else set begin=%curLine%
if %begin% gtr %lastLine% set /A begin=lastLine+1
set new=%begin%
rem Get new lines from keyboard until two spaces at end of line
echo Enter two spaces at end of line to end insert
:newLine
set "new[%new%]="
set "num= %new%"
set /P "new[%new%]=>%num:~-7%:*"
if "!new[%new%]:~-2!" equ " " goto endInsert
set /A new+=1
goto newLine
:endInsert
set "new[%new%]=!new[%new%]:~0,-2!"
if not defined new[%new%] set /A new-=1
rem Renumber lines from Begin to lastLine, if any
if %new% geq %begin% if %begin% leq %lastLine% (
set /A dest=new+1
call :copyLines %begin% %lastLine% !dest!
set /A curLine=new+1, lastLine=dest+lastLine-begin
) else (
set /A curLine=new+1, lastLine=new
)
rem Insert new text in the lines
for /L %%i in (%begin%,1,%new%) do set "line[%%i]=!new[%%i]!" & set "new[%%i]="
exit /B
List lines from Begin to End
:command-L [begin][,end]List
if not defined p1 ( rem Begin line omitted
if not defined p2 ( rem Both lines omitted
set /A begin=curLine-11
if !begin! lss 1 set begin=1
set /A end=begin+22
) else ( rem Just end line given
set /A begin=p2-22, end=p2
if !begin! lss 1 set begin=1
)
) else ( rem Begin line given
if not defined p2 ( rem Just begin line given
set /A begin=p1, end=p1+22
) else ( rem Both lines given
set /A begin=p1, end=p2
)
)
if %end% gtr %lastLine% set end=%lastLine%
for /L %%i in (%begin%,1,%end%) do (
if %%i equ %curLine% (set "num= %%i:*") else set "num= %%i: "
echo !num:~-10!!line[%%i]!
)
exit /B
Move lines from Begin to End at Dest
:command-m [begin],[end],destMove
set "p4="
call :command-C
set /A newBegin=end+1
if %newBegin% leq %lastLine% call :copyLines %newBegin% %lastLine% %begin%
set /A lastLine-=end-begin+1
exit /B
Paging lines from Begin for Range lines per page
:command-P [begin][,range]Paging
if defined p1 (
set begin=%p1%
) else (
if %curLine% equ 1 (
set begin=1
) else (
set /A begin=curLine+1
)
)
if defined p2 (set /A end=begin+p2-1) else set /A end=begin+22
if %end% gtr %lastLine% set end=%lastLine%
set curLine=%end%
for /L %%i in (%begin%,1,%end%) do (
if %%i equ %curLine% (set "num= %%i:*") else set "num= %%i: "
echo !num:~-10!!line[%%i]!
)
exit /B
Quit/No save
:command-Q Quit
choice /M "Abort edit "
if errorlevel 2 set "letter=X"
exit /B
Replace Text1 by Text2 in lines from Begin to End
:command-r [begin][,end][?]Rtext1[Ctrl-Ztext2]
exit /B
Search the Text in lines from Begin to End
:command-s [begin][,end][?]Stext
exit /B
Transfer (insert) text from Filename at Line
:command-t [line]Tfilename
exit /B
Show help on commands
:command-?
echo Edit line line#
REM echo Append [#lines]A
echo Copy [startline],[endline],toline[,times]C
echo Delete [startline][,endline]D
echo End (save file) E
echo Insert [line]I
ECHO Enter two spaces at end of line to end insert.
echo List [startline][,endline]L
echo Move [startline],[endline],tolineM
echo Page [startline][,endline]P
echo Quit (throw away changes) Q
echo Replace [startline][,endline][?]Roldtext[CTRL+Znewtext]
echo Search [startline][,endline][?]Stext
echo Transfer (insert file) [toline]T[drive:][path]filename
REM echo Write [#lines]W
exit /B
"CopyLines" auxiliary subroutine
:copyLines begin end dest
if %3 lss %1 (
set _dest=%3
for /L %%i in (%1,1,%2) do (
set "line[!_dest!]=!line[%%i]!"
set /A _dest+=1
)
) else (
set /A _dest=%3+%2-%1
for /L %%i in (%2,-1,%1) do (
set "line[!_dest!]=!line[%%i]!"
set /A _dest-=1
)
)
exit /B
@end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
Some parts of the code are not completely tested, so it is possible that some bugs appear. Please, report they. TIA