Nepedia
Back to Discover
Advanced Reader
💻

Macro (computer science)

technology Maturity 11-13 war conflict Next article →

This article covers sensitive topics: war_conflict. Parents can manage visibility in Parental Controls.

In computer science, a macro is a rule or a pattern. It specifies how a specific input should be mapped to a replacement output. This process of replacing the input with the output is known as macro expansion. The input and output can be many things. They might be a sequence of characters or lexical tokens. They can also be a syntax tree, which is a way of representing the structure of code. Macros are vital because they allow a programmer to turn a small sequence of characters into a big block of code. This makes programming tasks less tedious and helps prevent errors.

Jedit macro recorder
Jedit macro recorder

Macros work through a process of substitution. When a macro is triggered, the computer looks at the input and replaces it with the predefined output. Some macros are parameterless, meaning they always produce the exact same result. For example, in the C programming language, a macro might replace the name PI with the number 3.14159. Other macros are parameterized, which means they can take in specific arguments to change their output. This gives them power similar to a mathematical function. In the C language, a macro like pred(x) can take a value for x and subtract 1 from it. The final result depends entirely on what argument is passed to the macro.

There are several different types of macro systems used in computing. Text-substitution macros work at a basic level by swapping text. Languages like C and some assembly languages use these through a tool called a preprocessor. Syntactic macros are more advanced. They work at the level of abstract syntax trees rather than just simple text. This allows them to preserve the actual structure of the program more reliably. This style is very common in Lisp-like languages. These languages use a uniform, parenthesized syntax called S-expressions. This structure makes it easier for the computer to identify where a macro is being used.

Another important type is the procedural macro. In the PL/I language, these are written using a subset of the language itself. The compiler runs these statements during the compilation process. This creates a very powerful tool, though it can make the compiler larger and slower. Some macros are also designed to be "hygienic." This concept was introduced in papers during the mid-1980s. Hygienic macros use a pattern-based system to keep different parts of a program separate. This prevents "variable capture," which is when a macro accidentally interferes with a variable name used in the main program. This is now a standard feature in the Scheme programming language.

History shows how macros have evolved alongside computing. The term "macro" comes from "macro instruction." These were originally used to generate assembly language code. In 1963, Timothy Hart proposed adding macros to Lisp 1.5. This helped move the language toward more complex capabilities. In the 1980s, separate programs like SmartKey and SuperKey became very popular. These were used to automate keyboard and mouse actions. They could even format screenplays automatically. Later, as mouse-driven interfaces became common, these standalone programs mostly disappeared. Instead, macro features were built directly into applications like word processors and spreadsheets.

Macros are used in many famous software tools. The Emacs text editor is a major example. In fact, most of Emacs is actually made of macros. It was originally created using a language called TECO before being moved to Lisp. Another editor, Vim, allows users to record keyboard macros into a "register." These can then be replayed or edited. Microsoft Office also uses a language called Visual Basic for Applications, or VBA. VBA is very powerful because it can access many Windows system calls. However, this power also led to the rise of macro viruses in the mid-to-late 1990s. These viruses hide in documents and execute when the file is opened. Today, Microsoft uses updates and anti-virus software to stop these attacks.

Beyond programming, macros have a significant impact on digital environments. In massively multiplayer online role-playing games, or MMORPGs, players sometimes use keyboard macros. They use them to perform repetitive tasks to collect resources without human effort. This can actually hurt the game's economy. Because of this, most games list macro use as a violation of their terms of service. In the world of typesetting, the TeX system relies heavily on macros to function. Even the way we read manual pages on Unix systems often involves macro languages like troff or nroff. From small text swaps to building entire software suites, macros remain a fundamental tool in computing.

762 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (1)
File:Jedit macro recorder.png
Jedit macro recorder.png
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.