Macro
From Wikipedia, the free encyclopedia
-
For other uses, see Macro (disambiguation).
A macro in computer science is an abstraction, that defines how a certain input pattern is replaced by an output pattern according to a defined set of rules. There are three broad categories of macros, and each takes a different kind of input pattern to produce a different kind of output pattern.
Programming macros take a computer program as input and output a new, expanded program. Hence, this process is called macro-expansion and the tool that does this is called a macro-expander or a preprocessor. For many programming languages, a macro expander is included in the interpreter or compiler. These macros are typically used to enable a higher level of abstraction in programming languages.
Keyboard macros and application macros take a simple command as input and then output a new expanded set of commands or instructions. These macros are typically used to automate frequently-used sequences or lists of keystrokes or mouse movements.
Contents |
[edit] 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. Macros also make it possible to define data languages which are immediately compiled into code, which means that constructs such as state machines can be implemented in a way that is both natural and efficient.[1]
MacroML has typed syntax macros, and one productive way to think of these syntax macros is as a multi-stage computation.
[edit] 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.
[edit] Keyboard and mouse macros
Keyboard and mouse macros or definitions allow short sequences of keystrokes or mouse actions to substitute long sequences of commands, and can automate repetitive tasks. They can be created either by using an application's built-in macro features, or by means of a separate macro recorder.
During the 1980s, macro programs -- originally SmartKey, then Superkey, KeyWorks, Prokey -- were very popular, first as a means to automatically format screenplays, then for a variety of business tasks. Since the 1990s, keyboard macros are built into most popular applications, such as word processors and spreadsheets, but are more difficult to use than the older macro programs, and require knowledge of programming.
Keyboard macros have in more recent times come to life as a method of exploiting the economy of MMORPGs. By tirelessly performing a boring, repetitive, but low risk action, a player running a macro can earn a large amount of money. This effect is even larger when a macro-using player operates multiple accounts simultaneously, or operates the accounts for a large amount of time each day. As this money is generated without human intervention, it can dramatically upset the economy of the game by causing runaway inflation. For this reason, use of macros is universally a violation of the TOS or EULA, and administrators of MMORPGs fight a continual war to identify and punish macro users.
[edit] 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:
- C preprocessor
- Internet Macros for web browser automation (iMacros)
- M4 (mentioned previously, originally from AT&T, bundled with Unix)
- TRAC
- GPM
- PHP
- SMX
- Stage 2
- TeX
- ml/1
- Lisp has a powerful system of unhygienic syntax macros.
- Scheme has hygienic syntax macros with a macros by example system, and is probably the most advanced untyped macro system in common use.
- MacroML is an experimental system that seeks to reconcile static typing and macro systems.
- The General Purpose Macro Processor is a contextual pattern matching macro processor, which could be described as a combination of regular expressions, EBNF and awk
- MacroScript (Macro Scheduler)
- SAM76
Visual Basic Scripting (Microsoft Office) and OpenOffice.org Basic are commonly called "macros" but this is for historical reasons; they are not macro languages as defined in this article. Scripting languages such as Tcl and ECMAScript (a.k.a. ActionScript, DMDScript, JavaScript, JScript) have been embedded in applications and are sometimes called macro languages.
[edit] 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 viruses in VBA, commonly known as macro viruses. 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.