m4

Using m4 macros stored in one file on another file


I would like to store some elementary m4 macros that I typically use in a a separate file (say mymacros.m4 ) and then apply the macros used therein on the current source file I am working on mydoc.pre.txt

Is this possible? In all m4 tutorials, I have seen m4 macros being stored at the top of the file or interleaved with the working document. So at the commandline, I would like to type something like m4 -f mymacros.m4 mydoc.pre.txt > mydoc.post.txt.

Is there an m4 feature for this?


Solution

  • m4 provides include and sinclude macros (and maybe paste, spaste):

    include The defining text for the include macro shall be the contents of the file named by the first argument. It shall be an error if the file cannot be read. The behavior is unspecified if include is not immediately followed by a left-parenthesis.

    sinclude The sinclude macro shall be equivalent to the include macro, except that it shall not be an error if the file is inaccessible. The behavior is unspecified if sinclude is not immediately followed by a left- parenthesis.

    So you can include your mymacros.m4 into your m4 files:

    include(`mymacros.m4')
    

    You can use (if your m4 supports) the -I option of m4 command to add directories to include path.