Macro Article Index for
Macro
Articles about
Macro
 

Information About

Macro




A macro in Computer Science is an Abstraction , whereby a certain textual Pattern is replaced according to a defined set of rules. The Interpreter or Compiler automatically replaces the pattern when it is encountered. In compiled languages, ''macro-expansion'' always happens at compile-time. The tool which performs the expansion is sometimes called a ''macro-expander''. The term macro is used in many similar contexts which are derived from the concept of macro-expansion, including Keyboard Macros and Macro Languages . In most situations, the use of the word "macro" implies expanding a small command or action into a larger set of instructions.

The purpose of macros is to either Automate frequently-used Sequence s or enable a more powerful abstraction — but these are often the same thing.


PROGRAMMING MACROS


Languages such as C and Assembly Language have simple macro systems, implemented as preprocessors to the compiler or assembler. C preprocessor macros work by simple textual search-and-replace. More elaborate macros are available to C programmers by using an additional text-processing language such as M4 .

Lisp languages such as Common Lisp and Scheme have more elaborate macro systems: in Lisp, macros behave like functions transforming the program text itself, with the full language available to express such transformations. Whereas a C macro may define a simple replacement of one piece of syntax with another, a Lisp macro can control the evaluation of sections of code.

Being able to choose the order of evaluation (see Lazy Evaluation and Non-strict Functions ) enables the creation of new syntactic constructs (e.g. Control Structures ) indistinguishable from those built into the language. For instance, in a Lisp dialect that has cond but lacks if, it is possible to define the latter in terms of the former using macros. Entire major extensions to Lisp syntax, such as the CLOS system for Object-oriented Programming , have been defined as macros.

MacroML has typed syntax macros, and one productive way to think of these syntax macros is as a Multi-stage Computation .


APPLICATION MACROS


Application programs can also use an analogous system of macros, which allow the user to specify a series of (oft-repeated) actions as one new procedure. These application macros are generally recorded by carrying out the sequence once and letting the application "remember" the actions. More advanced users can often make use of a built-in macro programming language which has direct access to the features of the application.

When programming macros in an unfamiliar macro language, it may be helpful to record a macro to do what the user wants it to do, and then open the macro file and try to gain an understanding of how the command structure works. It is then possible to modify the commands to fine-tune it. Some macro languages, such as Great Plains accounting software's Dexterity runtime engine, lack the ability to import data from another data source, such as a comma delimited text file. This limitation can be overcome by creating a computer program in a more powerful programming language, such as VBA , to generate a specialized macro in the weaker programming language. For instance, a Microsoft Excel macro can be programmed to read data from a spreadsheet or text file and create a Great Plains .mac file designed to enter that specific data into Great Plains. A new .mac file would be needed to be generated for each new set of data.


KEYBOARD MACROS

Keyboard macros and editor macros are used interactively on a Graphical User Interface and Text Editor , respectively. These allow short sequences of keystrokes to substitute long sequences of commands, and can provide a simple form of Automation for Repetitive tasks.


MACRO LANGUAGES

A macro language is a programming language in which all or most computation is done by expanding macros. Macro languages are not widely used for general-purpose programming, but are common in text processing applications. Examples:


VISUAL BASIC FOR APPLICATIONS AND MACRO VIRUSES


Visual Basic For Applications (VBA) is a programming language included in Microsoft Office and some other applications. It is not, by the above definition, a macro language at all. However, its function has evolved from and replaced the macro languages which were originally included in some of these applications so it is popularly and mistakenly called a macro language.

VBA has access to most operating system functions and supports automatic execution when a document is opened. This makes it relatively easy to write Computer Virus es in VBA, commonly known as '' Macro Virus es''. In the mid-to-late 1990s , this became one of the most common types of computer virus. Some applications with macro languages, such as OpenOffice.org , deliberately exclude certain functionality (eg: automatic execution) to avoid such susceptibility. However, such features (in particular, automatic execution) remain popular in those applications that include them.


SEE ALSO