!BAT
# Input: (md)0x3000+0x50 holds menu
# Output: (md)0x3000+0x50 right justified menu
# menuwidth = RTL

if not exist RTL exit
set /a RTL=%RTL% > nul
if %RTL%<=10 exit

# clear all variables except RTL
setlocal & set * & set RTL=%RTL%

# LSTART                    TSTART   TEND           LEND
# v                         v        v              v  
# iftitle [sdsdsds] ^ctrl+A aaaaaaaa\n hhhhh\n hhhhh

# find title or iftitle [xxxxxx]  TSTART
# find cr or lf LEND
# find \n   TEND - if 0 or > LEND then TEND=TSTART

set /a PTR=0 > nul
set /a LSTART=0 > nul
set /a TSTART=0 > nul
set /a TEND=0 > nul
set /a LEND=0 > nul
cat --locate=\x00 --number=1 (md)0x3000+0x50 > nul
set /a BUFEND=%?%  > nul
set /a MDEND=%BUFEND%/0x200 > nul
set /a MDEND=%MDEND%+1 > nul
echo -e \x00\x00\x00\x00\x00 > (md)0x6000+0x50
echo -e \x00\x00\x00\x00\x00 > (md)0x6100+0x50

# --- LOOP TO FIND TITLE ENTRIES --- 
:lp
cat --locate=iftitle\x20[ --skip=%PTR% --number=1 (md)0x3000+%MDEND% > nul
set /a xif=%?% > nul
cat --locate=title\x20 --skip=%PTR% --number=1 (md)0x3000+%MDEND% > nul
set /a xt=%?% > nul
if %xt%>=%xif% set xt=%xif%
set LSTART=%xt%

set /a LEN=%BUFEND%-%PTR% > nul
# if no more send end of file to (md)0x6000
if %LEN%<=0 goto :FINISH
#echo if %LSTART%<=0 cat --skip=%PTR% --length=%LEN% (md)0x3000+%MDEND% >> (md)0x6000+0x50
if %LSTART%<=0 cat --skip=%PTR% --length=%LEN% (md)0x3000+%MDEND% >> (md)0x6000+0x50
if %LSTART%<=0 goto :FINISH

set /a LEN=%LSTART%-%PTR% > nul
if %LEN%<=0 goto :FINISH
#echo copy all from PTR=%PTR% to LSTART=%LSTART%  LEN=%LEN% to buffer
cat --skip=%PTR% --length=%LEN% (md)0x3000+0x50 >> (md)0x6000+0x50
set /a LSTART=%LSTART% > nul
#get line end
cat --locate=\x0d\x0a --skip=%LSTART%  --number=1 (md)0x3000+%MDEND% > nul
set /a LEND=%?% > nul

#fix for some lines may only end in LF!
cat --locate=\x0a --skip=%LSTART%  --number=1 (md)0x3000+%MDEND% > nul
set /a LF=%?% > nul
cat --locate=\x0d --skip=%LSTART%  --number=1 (md)0x3000+%MDEND% > nul
set /a CR=%?% > nul
if %CR%<=%LEND% set LEND=%CR%
if %LF%<=%LEND% set LEND=%LF%
set CR=
set LF=

if %LEND%<=0 set LEND=%BUFEND%
set /a LEN=%LEND%-%LSTART%  > nul
if %LEN%<=0 goto :FINISH

#copy line to (md)0x6100
cat --skip=%LSTART% --length=%LEN% (md)0x3000+%MDEND% > (md)0x6100+10
# MASSAGE LINE at 0x6100 HERE to right-justify
call :rtl
# OUTPUT LINE
cat (md)0x6100+10 >> (md)0x6000+0x50

set /a PTR=%LEND%+1 > nul
goto :lp

:FINISH
# all text copied to 0x6000
dd if=(md)0x6000+0x50 of=(md)0x3000+0x50 > nul
endlocal
exit


# --------- SUBROUTINE TO CONVERT TO RTL ---

:rtl
# (md)0x6100+10 contains entire title or iftitle line
#                           TSTART   TEND           LEND
# v                         v        v              v  
# iftitle [sdsdsds] ^ctrl+A aaaaaaaa\n hhhhh\n hhhhh

set /a t=999 > nul
# look for iftitle [ and then ] - if found move ptr  t to ] pos
cat --locate=iftitle\x20[ --number=1 (md)0x6100+0x10 > nul &; cat --locate=]\x20 --number=1 --skip=%?% (md)0x6100+0x10  > nul &; set /a t=%?%+1  > nul
if %t%==999 cat --locate=title\x20 --number=1 (md)0x6100+0x10 > nul &; set /a t=%?%+5  > nul
if %t%==999 goto :eof

# look for <space>^ - if found move ptr to ^ pos
cat --locate=\x20^ --number=1 --skip=%t% (md)0x6100+0x10 > nul |; goto :skp9
set /a t=%?%+1  > nul
cat --locate=\x20 --number=1 --skip=%t% (md)0x6100+0x10 > nul
set /a t=%?% > nul
:skp9

# look for non-space char
:lps
set /a l=0 > nul
cat --locate=\x20 --number=1 --length=1 --skip=%t% (md)0x6100+0x10 > nul &; set /a l=1  > nul
if l=1 set /a t=%t%+1 > nul && goto :lps

set /a TSTART=%t% > nul
# now find end of title
set TEND=0
cat --locate=\\n --number=1 --skip=%t% (md)0x6100+0x10  > nul &; set /a TEND=%?% > nul
if %TEND%<=0 cat --locate=\x00 --number=1 --skip=%t% (md)0x6100+0x10  > nul
if %TEND%<=0 set /a TEND=%?% > nul
set t=
set /a LEN=%TEND%-%TSTART% > nul
if %LEN%<=0 goto :eof

###echo ********
###cat --length=%TSTART% (md)0x6100+0x10
###echo
###cat --skip=%TSTART% --length=%LEN% (md)0x6100+0x10
###echo
###cat --skip=%TEND%  (md)0x6100+0x10
###echo

# output to 6110
echo > (md)0x6110+0x10
cat --length=%TSTART% (md)0x6100+0x10 >> (md)0x6110+0x10
# send title line to 6130
cat --skip=%TSTART% --length=%LEN% (md)0x6100+0x10 > (md)0x6130+0x10
# convert 6130 to 6120
call :RPAD1

#echo ########### && cat (md)0x6130+0x10 && echo && echo ----------- && cat (md)0x6120+0x10 && echo && echo ########### && pause

# add space after title
echo -e -n \x20 >> (md)0x6110+0x10
cat (md)0x6120+0x10 >> (md)0x6110+0x10

# rest of string is \nxxxx\nxxxxxx\nxxxxx help text
###cat --skip=%TEND%  (md)0x6100+0x10  >> (md)0x6110+0x10

cat --skip=%TEND% --locate=\x00 --number=1 (md)0x6100+0x10 > nul
set /a HEND=%?% > nul

# now do help text part of title line
:tnloop
set CR=0
cat --skip=%TEND% --locate=\\n --number=1 (md)0x6100+0x10 > nul && set CR=1
set /a TN=%?%+2 > nul
if %CR%==0 set TN=%HEND%
set /a LEN=%TN%-%TEND% > nul
if %LEN%<=0 goto :tnfin
cat --skip=%TEND% --length=%LEN% (md)0x6100+0x10 > (md)0x6130+0x10
# convert 6130 to 6120
call :RPAD1
#add 2 spaces to align under menu right hand edge
echo -n -e \x20\x20 >> (md)0x6110+0x10
#extra 2 if \n found
if %CR%==1 echo -n -e \x20\x20 >> (md)0x6110+0x10
cat (md)0x6120+0x10 >> (md)0x6110+0x10
set /a TEND=%TN% > nul
if %TEND%<=%HEND% goto :tnloop

:tnfin
echo >> (md)0x6110+0x10
dd if=(md)0x6110+0x10 of=(md)0x6100+0x10 > nul
set CR=
goto :eof


# --------- SUBROUTINE RPAD1 - right-pad a string with spaces depending on RTL value ------

:RPAD1

#expand variables etc. so get correct number of characters on line
cat (md)0x6130+0x10 | set A=
# add a space first
echo -n -e \x20  > (md)0x6130+0x10
call echo -n  %A% >> (md)0x6130+0x10

# String is in (md)0x6130+0x10 - convert into (md)0x6120+0x10
set r1=0

:r1lp
cat --locate=\x20 --skip=%r1% --number=1 --length=1 (md)0x6130+0x10 > nul &; set /a r1=%?%+1 > nul &; goto :r1lp

cat --locate=\x00 --number=1 (md)0x6130+0x10 > nul &; set /a te=%?% > nul

:r1lpe
set /a te=%te%-1 > nul
cat --locate=\x20 --skip=%te% --length=1 --number=1 (md)0x6130+0x10 > nul &; goto :r1lpe

set /a L=%te%-%r1% > nul
set /a L=%RTL%-%L%-2 > nul

#look for 3-byte UTF-8 characters and >0x80 2-byte sequences = BEWARE  %xxx%*2  doesnt work in batch file because %* is a special variable!
set UTF=0
cat --locate=\xEF\xBA (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%@retval%+%UTF% > nul
cat --locate=\xEF\xBB (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%@retval%+%UTF% > nul
cat --locate=\xDA (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xD9 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xD8 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xDB (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xCC (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xE2\x80 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%@retval%+%UTF% > nul
cat --locate=\xD7 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xD6 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xEF\xAC (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%@retval%+%UTF% > nul
cat --locate=\xD0 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xD1 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xD2 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\xCC (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
# add extra for \\ and \<space> in string
cat --locate=\x5c\x5c (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul
cat --locate=\x5c\x20 (md)0x6130+0x10 > nul
set /a UTF=%@retval%+%UTF% > nul


set /a L=%L%+%UTF% > nul
#if %UTF%>=1 cat --hex --length=0x40 (md)0x6130+10 && echo L=%L% %UTF%   RTL=%RTL%
set UTF=
echo -e -n \x00\x00 > (md)0x6120+0x10

:lp1s
if %L%>=0 echo -n -e \x20 >> (md)0x6120+0x10 && set /a L=%L%-1 > nul
if %L%>=0 goto :lp1s

set /a L=%te%-%r1%+1 > nul
cat --skip=%r1% --length=%L% (md)0x6130+0x10 >> (md)0x6120+0x10
set r1= && set te=

#cat (md)0x6120+0x10
#echo
#pause

goto :eof







