EDITOR batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

EDITOR batch

#1 Post by einstein1969 » 20 Jul 2014 02:51

Hi,

I need to realize an editor for my CMD files. I need in dos batch, no utility or external program

It is possible?

what are the limitation?

einstein1969

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: EDITOR batch

#2 Post by Squashman » 20 Jul 2014 09:28

You need to define editor.
Do you mean you want something like the old EDIT command in DOS?

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: EDITOR batch

#3 Post by einstein1969 » 20 Jul 2014 12:39

Squashman wrote:You need to define editor.
Do you mean you want something like the old EDIT command in DOS?


Yes, or old EDIT or an "advanced" EDLIN (fullscreen)

einstein1969

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: EDITOR batch

#4 Post by einstein1969 » 22 Jul 2014 13:20

There is a part that is not well implementable.

I know that the cursor keys are not utilizable. But it is possible using the ESC?

I used in past the editor VI and i think to implement in this mode for using cursor moviment.

Experts is necessary !

einstein1969

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: EDITOR batch

#5 Post by einstein1969 » 22 Jul 2014 13:45

This is an use of slice.cmd (from carlos)

Code: Select all

@echo off & setlocal DisableDelayedExpansion

if "%~1"=="" (goto :eof) else set "file=%~1"

set /a lines=43, cols=120

mode %cols%,%lines% & cls

set /a lines-=1, start=1, end=start+lines-1, from=0, len=cols-1

for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"

del "%tmp%\%file%.tmp"
set "Slice=call :slice %file% !start! !end! "%tmp%\%file%.tmp" !from! %len%"
set "Clear=set /p "=%BS%                                                       !CR!" <nul"

setlocal EnableDelayedExpansion

%Slice%

:loop_move

  set /p "=[WSAD] For move UP,DOWN,LEFT,RIGTH Q=QUIT %start% %end%!CR!" <nul

  for /F "eol=1delims=" %%x in ('xcopy /WQL "%~f0" NUL:\*') do (set "key=%%x" &  set "key=!key:~-1!")

  if /i "%key%"=="w" ( if !start! gtr 1 ( set /a end-=1, start-=1 & cls & %Slice% ) )
  if /i "%key%"=="s" ( %Clear% & set /a end+=1, start+=1 & call :slice %file% !end! !end! "%tmp%\%file%.tmp" !from! %len%)
  if /i "%key%"=="d" ( set /a from+=1 & cls& %Slice% )
  if /i "%key%"=="a" ( if !from! gtr 0 ( set /a from-=1 & cls & %Slice% ) )
  if /i "%key%"=="q" ( goto :eof )

goto :loop_move

goto :eof



rem serve il BACKSPACE
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"


:loop_getkey

    call :getkey
    if "%key%"==" " (set /p "=_%BS% " < nul) else set /p "=%key%" < nul

goto :loop_getkey


goto :eof

:getkey
  for /F "eol=1delims=" %%x in ('xcopy /WQL "%~f0" NUL:\*') do (set "key=%%x" &  set "key=!key:~-1!")
  if not defined key echo NO INPUT!
goto :eof


:slice
@Echo Off
:: Usage: slice.cmd filename [<int>start] [<int>end] [file_tmp] [from] [len]
:: Note: if end is 0 then prints until the end of file
:: Add trick for bigger files

Setlocal EnableExtensions DisableDelayedExpansion
Set /A b=%~2+0,e=%~3+0,#=0 2>Nul
If %b% Leq 0 Set "b=1"
If %e% Leq 0 Set "e=2147483647"

if not "%~4"=="" (set "tmp_file=%~4") else set "tmp_file=%tmp%\slice_%random%_%random%.dat"

if "%~4"=="" (
  Findstr /n "^" "%~dpf1">"%tmp_file%"
  rem Find /n /v "" "%~dpf1">"%tmp_file%" &:: più lento di findstr 
) else if not exist "%~4" Findstr /n "^" "%~dpf1">"%tmp_file%"


rem con il 'type %tmp_file%' ci mette troppo. Lo stesso come findstr.
rem For /f "delims=" %%$ In ('Findstr /n "^" "%~dpf1"') Do (

set "skip="
if %b% gtr 1 set /A "skip=%b%-1"
if defined skip (
  set /a #+=%skip%
  set "skip=skip=%skip%"
)

For /f "%skip% delims=" %%$ In (%tmp_file%) Do (
  Set /A #+=1 & Set "$=%%$"
  Setlocal EnableDelayedExpansion
    rem Set "$=!$:*:=!"
    If !#! Geq %b% Echo(!$:~%5,%6!
    If !#! Geq %e% (Endlocal & Goto End)
    rem if !random! geq 32700 title !#!
  Endlocal
)

:End
Endlocal & Goto:Eof


The edit phase is not supported.

einstein1969

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: EDITOR batch

#6 Post by Aacini » 09 Aug 2014 22:13

Another crazy Batch file project :?: I like it! :D

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

Antonio

Post Reply