Using Macro Processing as a Powerful Writing Tool

Do you have difficulty keeping up with a lecturer while taking notes?

Don't want to spend hours translating your shorthand?

Sick of typing long or complex syntaxes?

Then Macro Processing is for you!

Introduction

Macro processing, in the most simplest terms, is taking text, and transforming it. I guess you could also call this Text Transforming. So why would you ever want transform text? Mostly to save time, and make certain tasks easier. After reading this you'll wonder how you ever survived without one!

In particular we will be looking at the magnificent macro processing application called m4. A secret agent in disguise. Not many people know of its power. It hides in the shadows as a configuration script generator.

Lets Do Stuff

So lets get to business! Here is how m4 generally works:
  1. It takes what you type or have written in a file.
  2. If what you typed was a macro, it will transform it.
  3. If what you type was a macro definition, it will define a new macro.
  4. It will then push any transformations to another program or a file.
This is particularly useful when you want to avoid typing the same piece of text (or as I'll be calling it, an expression) over and over again.

Real Life (!) Examples

Here is a useful example of using m4. You decide to type in shorthand to save time while taking notes. You might forget what your shorthand means later though, or you want someone else to read your notes. With m4, this isn't a problem. You can define macros which are just text.

Lets define the following macros in a file called shorthand.m4:
define(`lol', `hahahaha')
define(`iou', `I owe you')
define(`asap', `as soon as possible')

Ok so now when we type iou it will automagically be transformed into "I owe you". Cool. Lets take it a step further. Lets say you don't want to type define(`token',`replacement') all the time. What can we do to solve this?! Macros.

So lets define a macro that takes 2 arguments (hey this like that JavaScript stuff!!), and inserts them into our replacement:
define(`d', `define($1, $2)')

We then run this through/over our shorthand.m4 file, that now looks like this:
d(`lol', `hahahaha')
d(`iou', `I owe you')
d(`asap', `as soon as possible')

To get what we had originally.


For Programmers

So that was a really simple example. Lets say  you are sick of a certain programming language's for loop. You want to write a foreach-like loop with ease. We can easily define a macro for that!

define(`foreach', `for($2 = $1; $2 != NULL; $2++)')

Now we can write a foreach-like loop...

foreach(`cats', `cat') {
    // Do the thing Zhu Li!
}

Of course though I recommend using the programming language's preprocessor if it has one, and not m4! :)


For Ed Users (hah)

m4 -i | ed

Boom, you can suddenly have macro abilities in ed! (Thanks #ed!)

Here is one simple example of a m4ed (hah, get it? because only a tipsy 1337 haxxor programmer would ever do this...)  macro that I created:

define(`al', `s/$/')

Which when used:

1 al this is appended text to the first line of the file!

Will append text to a line.

Conclusion

There you have it. You've successfully learned how to use another very convenient tool for every day use. Now go use it. Spread the word. Most importantly, show us your cool macros!

Comments

Popular Posts