3

Is there a good general strategy for using m4 with Python? Specifically, Python's whitespace requirements make using m4 somewhat awkward. For example, the following program:

def foo():
    pushdef(`X',`    $1 = $2')
    include(test01.def)
    popdef(`X')

foo()

with test01.def as

X(x,1)
X(y,2)
X(z,3)

Generates the python program:

def foo():

        x = 1
    y = 2
    z = 3



foo()

As such, the indentation is off. Certainly, we can fix this with

def foo():
pushdef(`X',`    $1 = $2')
include(test01.def)
popdef(`X')

foo()

However, then I feel the file that we edit becomes ugly because it's harder to track the indentation in our code organization. Really, what I'd like the ability to do is to use the first program and have m4 eat the leading whitespace before the include. I'm not sure if it's possible for m4 to eat the leading whitespace.

In addition, I'm aware that that there are Python specific macro utilities. However, I am not interested in using these. I need to use these X macros in a number of different languages such as in C and LaTeX and it is my intention to use m4 in each of these cases since it's readily available and language agnostic. As such, I'm really looking for an m4 solution if one is possible.

wyer33
  • 6,060
  • 4
  • 23
  • 53

1 Answers1

1

Would something like the following be acceptable?

define(INCLUDE_ON_NEW_LINE, `
include($1)')

`def foo():'
    pushdef(`X',`    $1 = $2')
    INCLUDE_ON_NEW_LINE(`test01.def')
    popdef(`X')

`foo()'

You can place an extra dnl on the previous line if you find that output more pleasing. Not all of my quotes are needed but I like to be cautious.

But honestly, I find your “ugly” solution easier to understand. Why should the M4 code be indented like Python code?

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
  • The indentation is a preference thing. Really, it all works, but it's easier for me to see things indented. – wyer33 Mar 12 '15 at 22:23