Macro
Encyclopedia
A macro in computer science
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...

 is a rule or pattern
Pattern
A pattern, from the French patron, is a type of theme of recurring events or objects, sometimes referred to as elements of a set of objects.These elements repeat in a predictable manner...

 that specifies how a certain input sequence (often a sequence of characters
Character (computing)
In computer and machine-based telecommunications terminology, a character is a unit of information that roughly corresponds to a grapheme, grapheme-like unit, or symbol, such as in an alphabet or syllabary in the written form of a natural language....

) should be mapped to an output sequence (also often a sequence of characters) according to a defined procedure. The mapping process that instantiates (transforms) a macro into a specific output sequence is known as macro expansion.

The term is used to make available to the programmer
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

, a sequence of computing instructions as a single program statement, making the programming task less tedious and less error-prone. (Thus, they are called "macros" because a big block of code can be expanded from a small sequence of characters). Macros often allow positional or keyword parameters that dictate what the conditional assembler
Conditional assembly language
A Conditional Assembly Language is that part of an Assembly Language used to write macros.- Example :In the IBM conditional assembly language, the most important statements are:-* MACRO and MEND - used to start and finish a macro...

 program generates and have been used to create entire programs
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 or program suites according to such variables as operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

, platform or other factors.

Keyboard and mouse macros

Keyboard macros and mouse macros allow short sequences of keystrokes and mouse actions to be transformed into other, usually more time-consuming, sequences of keystrokes and mouse actions. In this way, frequently used or repetitive sequences of keystrokes and mouse movements can be automated. Separate programs for creating these macros are called macro recorder
Macro recorder
A macro recorder is a piece of software that "records" user actions for "playback at a later time".The main advantage of using a macro recorder is that it allows a user to easily perform complex operations much faster and with less effort without requiring custom computer programming / scripting.-...

s.

During the 1980s, macro programs – originally SmartKey
SmartKey
SmartKey was the first macro processing program of its type, and the first TSR or terminate and stay resident program for PCs and their eight bit predecessors....

, then SuperKey
Superkey
A superkey is defined in the relational model of database organization as a set of attributes of a relation variable for which it holds that in all relations assigned to that variable, there are no two distinct tuples that have the same values for the attributes in this set...

, KeyWorks, Prokey – were very popular, first as a means to automatically format screenplay
Screenplay
A screenplay or script is a written work that is made especially for a film or television program. Screenplays can be original works or adaptations from existing pieces of writing. In them, the movement, actions, expression, and dialogues of the characters are also narrated...

s, then for a variety of user input tasks. These programs were based on the TSR (Terminate and stay resident) mode of operation and applied to all keyboard input, no matter in which context it occurred. They have to some extent fallen into obsolescence following the advent of mouse-driven user interface and the availability of keyboard and mouse macros in applications such as word processors and spreadsheets, making it possible to create application-sensitive keyboard macros.

Keyboard macros have in more recent times come to life as a method of exploiting the economy of massively multiplayer online role-playing game (MMORPG)
MMORPG
Massively multiplayer online role-playing game is a genre of role-playing video games in which a very large number of players interact with one another within a virtual game world....

s. By tirelessly performing a boring, repetitive, but low risk action, a player running a macro can earn a large amount of the game's currency or resources. 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. For this reason, use of macros is a violation of the TOS
Terms of Service
Terms of service are rules which one must agree to abide by in order to use a service. Unless in violation of consumer protection laws, such terms are usually legally binding...

 or EULA of most MMORPGs, and administrators of MMORPGs fight a continual war to identify and punish macro users.

Application macros and scripting

Keyboard and mouse macros that are created using an application's built-in macro features are sometimes called application macros. They are created by carrying out the sequence once and letting the application record the actions. An underlying macro programming language, most commonly a scripting language
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...

, with direct access to the features of the application may also exist.

The programmers' text editor Emacs
Emacs
Emacs is a class of text editors, usually characterized by their extensibility. GNU Emacs has over 1,000 commands. It also allows the user to combine these commands into macros to automate work.Development began in the mid-1970s and continues actively...

 (short for "editing macros") follows this idea to a conclusion. In effect, most of the editor is made of macros. Emacs was originally devised as a set of macros in the editing language TECO
Text Editor and Corrector
TECO is a text editor originally developed at the Massachusetts Institute of Technology in the 1960s, after which it was modified by 'just about everybody'...

; it was later ported to dialects of Lisp.

Another programmer's text editor, Vim
Vim (text editor)
Vim is a text editor written by Bram Moolenaar and first released publicly in 1991. Based on the vi editor common to Unix-like systems, Vim is designed for use both from a command line interface and as a standalone application in a graphical user interface...

 (a descendant of vi
Vi
vi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by the Single Unix Specification and POSIX.The original code for vi...

), also has full implementation of macros. It can record into a register (macro) what a person types on the keyboard and it can be replayed or edited just like VBA macros for Microsoft Office. Vim also has a scripting language called Vimscript
Vimscript
Vimscript, also Vim script, is the scripting language built into Vim. Based on the ex editor language of the original vi editor, early versions of Vim added commands for control flow and function definitions. Since version 7, Vimscript also supports more advanced data types such as Lists and...

 to create macros.

Visual Basic for Applications
Visual Basic for Applications
Visual Basic for Applications is an implementation of Microsoft's event-driven programming language Visual Basic 6 and its associated integrated development environment , which are built into most Microsoft Office applications...

 (VBA) is a programming language included in Microsoft Office
Microsoft Office
Microsoft Office is a non-free commercial office suite of inter-related desktop applications, servers and services for the Microsoft Windows and Mac OS X operating systems, introduced by Microsoft in August 1, 1989. Initially a marketing term for a bundled set of applications, the first version of...

 and some other applications. However, its function has evolved from and replaced the macro languages that were originally included in some of these applications.

Macro virus

VBA
Visual Basic for Applications
Visual Basic for Applications is an implementation of Microsoft's event-driven programming language Visual Basic 6 and its associated integrated development environment , which are built into most Microsoft Office applications...

 has access to most Microsoft Windows system calls and executes when documents are opened. This makes it relatively easy to write computer virus
Computer virus
A computer virus is a computer program that can replicate itself and spread from one computer to another. The term "virus" is also commonly but erroneously used to refer to other types of malware, including but not limited to adware and spyware programs that do not have the reproductive ability...

es in VBA, commonly known as macro virus
Macro virus (computing)
In computing terminology, a macro virus is a virus that is written in a macro language: that is to say, a language built into a software application such as a word processor...

es. In the mid-to-late 1990s, this became one of the most common types of computer virus. However, during the late 1990s and to date, Microsoft
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...

 has been patching and updating their programs. In addition, current anti-virus programs immediately counteract such attacks.

Text substitution macros

Languages such as C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 and assembly language
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...

 have rudimentary macro systems, implemented as preprocessor
Preprocessor
In computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers...

s to the compiler or assembler. C preprocessor
C preprocessor
The C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....

 macros work by simple textual search-and-replace at the token, rather than the character, level.
A classic use of macros is in the computer typesetting system TeX
TeX
TeX is a typesetting system designed and mostly written by Donald Knuth and released in 1978. Within the typesetting system, its name is formatted as ....

 and its derivatives, where most of the functionality is based on macros.
MacroML
MacroML
MacroML is an experimental programming language based on the ML programming language family that seeks to reconcile ML's static typing systems, and the types of macro systems more commonly found in dynamically typed languages like Scheme; this reconciliation is difficult as macro transformations...

 is an experimental system that seeks to reconcile static typing and macro systems. Nemerle has typed syntax macros, and one productive way to think of these syntax macros is as a multi-stage computation
Metaprogramming
Metaprogramming is the writing of computer programs that write or manipulate other programs as their data, or that do part of the work at compile time that would otherwise be done at runtime...

.
Other examples:
  • m4
    M4 (computer language)
    m4 is a general purpose macro processor designed by Brian Kernighan and Dennis Ritchie. m4 is an extension of an earlier macro processor m3, written by Ritchie for the AP-3 minicomputer.-Use:...

     is a sophisticated, stand-alone, macro processor.
  • TRAC
    TRAC programming language
    TRAC is a programming language developed in the early 1960s by Calvin Mooers. It was one of three "first languages" recommended by Ted Nelson in Computer Lib....

  • Macro Extension TAL
    Metal
    A metal , is an element, compound, or alloy that is a good conductor of both electricity and heat. Metals are usually malleable and shiny, that is they reflect most of incident light...

    , accompanying Template Attribute Language
    Template Attribute Language
    The Template Attribute Language is a templating language used to generate dynamic HTML and XML pages. Its main goal is to simplify the collaboration between programmers and designers...

  • SMX, for web pages
  • ML/1
    ML/I
    ML/1 is a powerful general purpose macro processor.Typical uses of ML/1 include:* editing, modifying, correcting, or reformatting text files* translating source code from one programming language to another...

     Macro Language One
  • The General Purpose Macroprocessor is a contextual pattern matching macro processor, which could be described as a combination of regular expression
    Regular expression
    In computing, a regular expression provides a concise and flexible means for "matching" strings of text, such as particular characters, words, or patterns of characters. Abbreviations for "regular expression" include "regex" and "regexp"...

    s, EBNF and AWK
    AWK (programming language)
    The AWK utility is a data extraction and reporting tool that uses a data-driven scripting language consisting of a set of actions to be taken against textual data for the purpose of producing formatted reports...

  • SAM76
    SAM76
    SAM76 is a macro programming language used from the late 1970s to the present 2007 initially ran on CP/M.The SAM76 language is a list and string processor designed for interactive and user-directed applications, including artificial intelligence programming, and permits high portability from...

  • minimac, a concatenative macro processor.
  • troff
    Troff
    troff is a document processing system developed by AT&T for the Unix operating system.-History:troff can trace its origins back to a text formatting program called RUNOFF, written by Jerome H. Saltzer for MIT's CTSS operating system in the mid-1960s...

     and nroff
    Nroff
    nroff is a Unix text-formatting program; it produces output suitable for simple fixed-width printers and terminal windows...

    , for typesetting and formatting Unix manpages.

Embeddable languages

Some languages, such as PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

, can be embedded in free-format text, or the source code of other languages. The mechanism by which the code fragments are recognised (for instance, being bracketed by and ?>) is similar to a textual macro language, but they are much more powerful, fully featured languages.

Procedural macros

Macros in the PL/I
PL/I
PL/I is a procedural, imperative computer programming language designed for scientific, engineering, business and systems programming applications...

 language are written in a subset of PL/I itself: the compiler executes "preprocessor
Preprocessor
In computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers...

 statements" at compilation time, and the output of this execution forms part of the code that is compiled. The ability to use a familiar procedural language as the macro language gives power much greater than that of text substitution macros, at the expense of a larger and slower compiler.

Frame Technology
Frame Technology (software engineering)
Frame technology is a language-neutral system that manufactures custom software from reusable, machine-adaptable building blocks, called frames. FT is used to reduce the time, effort, and errors involved in the design, construction, and evolution of large, complex software systems...

's frame macros have their own command syntax but can also contain text in any language. Each frame is both a generic component in a hierarchy of nested subassemblies, and a procedure for integrating itself with its subassembly frames (a recursive process that resolves integration conflicts in favor of higher level subassemblies). The outputs are custom documents, typically compilable source modules. Frame Technology can avoid the proliferation of similar but subtly different components, an issue that has plagued software development since the invention of macros and subroutines.

Most assembly languages have less powerful procedural macro facilities, for example allowing a block of code to be repeated N times for loop unrolling; but these have a completely different syntax from the actual assembly language.

Lisp / S-expression macros

Lisp's uniform, parenthesized syntax (known as S-Expression
S-expression
S-expressions or sexps are list-based data structures that represent semi-structured data. An S-expression may be a nested list of smaller S-expressions. S-expressions are probably best known for their use in the Lisp family of programming languages...

s) works especially well with macros. Languages of the Lisp family, such as Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

, Clojure
Clojure
Clojure |closure]]") is a recent dialect of the Lisp programming language created by Rich Hickey. It is a general-purpose language supporting interactive development that encourages a functional programming style, and simplifies multithreaded programming....

, Scheme, and Racket, have powerful macro systems because the syntax is simple enough to be parsed easily, making it possible to regard programs as input and output data for program transformers. Lisp macros transform the program structure itself, with the full language available to express such transformations.

The earliest Lisp macros took the form of FEXPRs
Fexpr
In Lisp programming languages, a fexpr is a function whose operands are passed to it without being evaluated. When a fexpr is called, only the body of the fexpr is evaluated; no other evaluations take place except when explicitly initiated by the fexpr...

, function-like operators whose inputs were not the values computed by the arguments but rather the syntactic forms of the arguments, and whose output were syntactic fragments to be used in place of the FEXPR's use.

In later Lisps, FEXPRs were replaced by DEFMACRO, a system that allowed programmers to specify source-to-source transformations that were applied before the program is run.

In the mid-eighties, a number of papers introduced the notion of hygienic macro expansion (syntax-rules), a pattern-based system where the syntactic environments of the macro definition and the macro use are distinct, allowing macro definers and users not to worry about inadvertent variable capture (cf. Referential transparency).

Macro systems have a range of uses. Being able to choose the order of evaluation (see lazy evaluation
Lazy evaluation
In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...

 and non-strict functions
Strict function
A strict function in the denotational semantics of programming languages is a function f where f\left = \perp. The entity \perp, called bottom, denotes an expression which does not return a normal value, either because it loops endlessly or because it aborts due to an error such as division by...

) 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.

Next, macros make it possible to define data languages that 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.

Macros can also be used to introduce new binding constructs. The most well-known example is the transformation of let into the application of a function to a set of arguments.

Felleisen
Matthias Felleisen
Matthias Felleisen is a computer science professor and an author of German background.Felleisen is currently a Trustee Professor in the College of Computer and Information Science at Northeastern University in Boston, Massachusetts. In the past he has taught at Rice University after receiving his...

 conjectures that these three categories make up the primary legitimate uses of macros in such a system.

The Racket language extends the notion of a macro system to a syntactic tower, where macros can be written in languages including macros, using hygiene to ensure that syntactic layers are distinct and allowing modules to export macros to other modules.

Macros for machine-independent software

Macros are normally used to map a short string (macro invocation) to a longer sequence of instructions. Another, less common, use of macros is to do the reverse: to map a sequence of instructions to a macro string. This was the approach taken by the STAGE2 Mobile Programming System, which used a rudimentary macro compiler (called SIMCMP) to map the specific instruction set of a given computer to counterpart machine-independent macros. Applications (notably compilers) written in these machine-independent macros can then be run without change on any computer equipped with the rudimentary macro compiler. The first application run in such a context is a more sophisticated and powerful macro compiler, written in the machine-independent macro language. This macro compiler is applied to itself, in a bootstrap fashion, to produce a compiled and much more efficient version of itself. The advantage of this approach is that complex applications can be ported from one computer to a very different computer with very little effort (for each target machine architecture, just the writing of the rudimentary macro compiler). The advent of modern programming languages, notably C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, for which compilers are available on virtually all computers, has rendered such an approach superfluous. This was, however, one of the first instances (if not the first) of compiler bootstrapping
Bootstrapping (compilers)
In computer science, bootstrapping is the process of writing a compiler in the target programming language which it is intended to compile...

.

Macro library

A macro library is a collection of macros.
The library allows users to download dynamic macros that will work on any suitable computer.
By using the macros in the library, the user can automate action on his computer.
Disk defragmentation, virus scan, can be executed automatically.
Popular macros libraries - http://macrorecordings.com/, http://macrosland.com/

See also

  • Anaphoric macro
    Anaphoric macro
    An anaphoric macro is a type of programming macro that deliberately captures some form supplied to the macro which may be referred to by an anaphor...

    s
  • Hygienic macro
    Hygienic macro
    Hygienic macros are macros whose expansion is guaranteed not to cause collisions with existing symbol definitions. They are a feature of programming languages such as Scheme and Dylan.-The hygiene problem:...

    s
  • Macro instruction
    Macro instruction
    A macro instruction is a line of computer program coding that results in one or more lines of program coding in the target programming language, sets variables for use by other statements, etc....

  • Programming by demonstration
    Programming by demonstration
    In computer science, programming by demonstration is an End-user development technique for teaching a computer or a robot new behaviors by demonstrating the task totransfer directly instead of programming it through machine commands....

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK