'Free WordBasic Macro to make C programming code look nicer when printed.
'(for Word for Windows 2)

' Instructions: open this document in MS-Word for Windows, select the whole document
' and copy it to the clipboard. Open a new macro (i.e. the 'Tools' menu, 'Macro...'), call it
' whatever you like. Paste this document into that macro, and voila! there you have it.
' I have found that it is more useful if you then give the macro a command key equivalent, or put 
' a tool in the Toolbar (I use the capital 'C' so that I know that it is for my 'C' programs).

' Written by Philip Ryan, DSTO Materials Research Laboratory, Melbourne, Australia.
' email: ryanph@mrl.dsto.gov.au

' Do anything you like with this, if you make it better, send me a copy.
' Note that this could easily be converted to use with Pascal or any other language.

Sub MAIN

' The StartOfDocument statement is needed to ensure that the ReplaceAll works properly.
StartOfDocument

' For the first set of words, make the format of the replaced text be bold only.
EditReplaceChar .Bold = 1, .Italic = 0, .Underline = 0, .Strikeout = 0

' Start doing the replacing.
EditReplace .Find = "float", .Replace = "float", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "long", .Replace = "long", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "int", .Replace = "int", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "double", .Replace = "double", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "if", .Replace = "if", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "then", .Replace = "then", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "else", .Replace = "else", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "while", .Replace = "while", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "until", .Replace = "until", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "repeat", .Replace = "repeat", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "return", .Replace = "return", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "break", .Replace = "break", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "case", .Replace = "case", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "switch", .Replace = "switch", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "repeat", .Replace = "repeat", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "register", .Replace = "register", .Format = 1, .WholeWord = 1, 
.ReplaceAll
StartOfDocument
EditReplace .Find = "volatile", .Replace = "volatile", .Format = 1, .WholeWord = 1, 
.ReplaceAll
StartOfDocument
EditReplace .Find = "union", .Replace = "union", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "typedef", .Replace = "typedef", .Format = 1, .WholeWord = 1, 
.ReplaceAll
StartOfDocument
EditReplace .Find = "struct", .Replace = "struct", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "auto", .Replace = "auto", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "const", .Replace = "const", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "continue", .Replace = "continue", .Format = 1, .WholeWord = 1, 
.ReplaceAll
StartOfDocument
EditReplace .Find = "default", .Replace = "default", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "enum", .Replace = "enum", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "extern", .Replace = "extern", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "goto", .Replace = "goto", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "short", .Replace = "short", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "signed", .Replace = "signed", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "unsigned", .Replace = "unsigned", .Format = 1, .WholeWord = 1, 
.ReplaceAll
StartOfDocument
EditReplace .Find = "sizeof", .Replace = "sizeof", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "static", .Replace = "static", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "void", .Replace = "void", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "FILE", .Replace = "FILE", .Format = 1, .WholeWord = 1, .MatchCase = 
1, .ReplaceAll

'For this next set, which is a small subset of the standard libraries, make the replaced text 
'format be italic only.

EditReplaceChar .Bold = 0, .Italic = 1, .Underline = 0, .Strikeout = 0
StartOfDocument
EditReplace .Find = "main", .Replace = "main", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "printf", .Replace = "printf", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "fprintf", .Replace = "fprintf", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "scanf", .Replace = "scanf", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "fscanf", .Replace = "fscanf", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "getc", .Replace = "getc", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "getch", .Replace = "getch", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "getchar", .Replace = "getchar", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "putc", .Replace = "putc", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "putch", .Replace = "putch", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "putchar", .Replace = "putchar", .Format = 1, .WholeWord = 1, 
.ReplaceAll

StartOfDocument

' For the last set, the C pre-processor directives, make the format bold-italic only.
EditReplaceChar .Bold = 1, .Italic = 1, .Underline = 0, .Strikeout = 0
EditReplace .Find = "#include", .Replace = "#include", .Format = 1, .WholeWord = 1, 
.ReplaceAll
StartOfDocument
EditReplace .Find = "#define", .Replace = "#define", .Format = 1, .WholeWord = 1, 
.ReplaceAll
StartOfDocument
EditReplace .Find = "#ifdef", .Replace = "#ifdef", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "#error", .Replace = "#error", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "#if", .Replace = "#if", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "#ifndef", .Replace = "#ifndef", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "#else", .Replace = "#else", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "#elif", .Replace = "#elif", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "#endif", .Replace = "#endif", .Format = 1, .WholeWord = 1, .ReplaceAll
StartOfDocument
EditReplace .Find = "#pragma", .Replace = "#pragma", .Format = 1, .WholeWord = 1, 
.ReplaceAll

' Now, to format the comments.
StartOfDocument

' Look for the normal comment string delimiter of C.
EditFind .Find = "/*",  .Direction = 2

' Keep on searching for comment blocks.
While EditFindFound()
	ExtendSelection "*"
	ExtendSelection "/"
	FormatCharacter .Italic = 1, .Bold = 0
	Cancel
' To ensure that the next found character is in the _next_ comment block,
' we set the findchar type to italic _off_. (Since otherwise the insertion
' point always remains at the start of the last selection.)
	EditFindChar .Italic = 0
	EditFind
Wend

End Sub



