linefeed in macro AND memory problem.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

linefeed in macro AND memory problem.

#1 Post by Ed Dyreen » 06 Sep 2012 16:56

'
Trying to find a way that allows linefeeds inside macros that do not appear in memory.

Code: Select all

@echo off &prompt $G &echo.


set ^"_v=for %%? in (1,2) do if %%?==2 (^
%=   a block follows   =%
%=      =%echo.contents^
%=   a block has ended =%
)else set $="

set _v

pause
exit

Code: Select all

_v=for %? in (1,2) do if %?==2 (
echo.contents
)else set $=
Druk op een toets om door te gaan. . .
That would allow me to space blocks of code while defining but have it all on one line in memory.

Code: Select all

>set _v
_v=for %? in (1,2) do if %?==2 (echo.contents)else set $=

>
I studied The BatchLineParser tutorial ( jeb, dBenham )

without luck :(

Normally you would add a caret to the end of the line.
But the first character is handled as an escaped character.
And if I double the caret, it appears inside the variable.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: linefeed in macro AND memory problem.

#2 Post by Ed Dyreen » 06 Sep 2012 17:32

'
Some progress, I seem to get it working in enableDelayed

Code: Select all

@echo on &prompt $G &set verify=works &set $lf=^


::
@setlocal enableDelayedExpansion &echo. &echo. &echo.test1

%=   =%set ^"_v=for %%? in (1,2) do @if %%?==2 (^
!=this is a=^^^!^
!=comment  =^^^!^
%=      =%echo.it ^^^!verify^^^!^&^
!==^^^!^
%=   =%endlocal)else setlocal enableDelayedExpansion^&set $="

@echo. &set _v
@echo. &%_v%


pause
exit

Code: Select all

test1

> set "_v=for %? in (1,2) do @if %?==2 (!=this is a     =^!!=comment    =^!echo.it ^!verify^!&!==^!endlocal)else setlocal enableDelayedExpansion&set $="

_v=for %? in (1,2) do @if %?==2 (echo.it !verify!&endlocal)else setlocal enableDelayedExpansion&set $=

it works

> pause
Druk op een toets om door te gaan. . .
Still no luck in disableDelayed :(

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: linefeed in macro AND memory problem.

#3 Post by jeb » 07 Sep 2012 01:52

Hi Ed,

Ed Dyreen wrote:Normally you would add a caret to the end of the line.
But the first character is handled as an escaped character.

I don't see any problem here, as you don't need the first character in the line to be working in the moment of defining the macro.
And after setting the macro, all characters work as expected.

And the percents (your comments) aren't affected by the caret at all, as they are expanded before the escaping is effective.

hope it helps
jeb

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: linefeed in macro AND memory problem.

#4 Post by Ed Dyreen » 07 Sep 2012 02:20

'
It helps,

But I still have the problem that the empty lines become part of the variable.
And the most obvious solution just puts a caret and a linefeed in memory.

Code: Select all

@echo on &prompt $G &set verify=works

@setlocal disableDelayedExpansion &echo. &echo. &echo.test1

%=   =%set ^"_v=for %%? in (1,2) do @if %%?==2 (^
^  << this caret is placed in memory corrupting the macro.
%=      =%echo.it ^^^!verify^^^!^&^
^  << linefeeds are still placed in memory too.
%=   =%endlocal)else setlocal enableDelayedExpansion^&set $="

@echo. &set _v
@echo. &%_v%


pause
exit
It's not a big deal as I got it the way I want in enableDelayed and only use disableDelayed macros early in the batch.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: linefeed in macro AND memory problem.

#5 Post by Liviu » 10 Sep 2012 19:54

Sorry, no real answer to the original question, but just as a thought... Macros suffer IMHO of a readability problem, and as long as the source functions are used as the definition there is no easy way out. What if, very loosely speaking, a macro could be written as a more readable "source" (possibly following certain constraints), then read and parsed off an external file into the actual "executable" macro. I, for one, would gladly trade off the extra step of reading an additional file against the advantage of being able to still follow next week what on earth it's exactly doing ;-) Ideally, said file would be close to regular batch syntax, so for example one could take an existing .cmd (or :sub thereof) and "compile" it into a macro at runtime as needed.

Liviu

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: linefeed in macro AND memory problem.

#6 Post by Ed Dyreen » 19 Sep 2012 08:41

Liviu wrote:What if, very loosely speaking, a macro could be written as a more readable "source" (possibly following certain constraints), then read and parsed off an external file into the actual "executable" macro.
I plan something more viable, to dereference/isolate variables, so I can post them as one single variable without any references.
Liviu wrote:I, for one, would gladly trade off the extra step of reading an additional file against the advantage of being able to still follow next week what on earth it's exactly doing ;-)
Which is why I am trying to create separated blocks of code.

Code: Select all

%=      =%codeBlock%=   blocks of code can be separated   =%^

%=      =%codeBlock%=   but the linefeeds become part of the variable which is undesireable    =%^

%=      =%codeBlock%=   blocks of code can be separated   =%^

Code: Select all

%=      =%codeBlock%=   blocks of code can be separated   =%^
!==^^^!^
%=      =%codeBlock%=   this way is to be preferred, the linefeeds don't become part of the variable    =%^
%=      =%codeBlock%=   unfortunately this only works for enableDelayed definitions   =%^
!==^^^!^
%=      =%codeBlock%=   blocks of code can be separated   =%^
Don't see the benefit from compiling functions to macros, I'll post a good macro tutorial someday !

Post Reply