Prettyprint
Encyclopedia
Prettyprint is the application of any of various stylistic formatting conventions to text, source code
, markup
, and other similar kinds of content. These formatting conventions usually consist of changes in positioning, spacing, color, contrast, size and similar modifications intended to make the content easier for people to view, read, and understand. Prettyprinters for programming language source code are sometimes called code beautifiers or syntax highlighters
.
professionally. For example, in computer algebra system
s such as Maxima or Mathematica
the system may write output like "x ^ 2 + 3 * x" as "". Some graphing calculator
s, such as the Casio 9860 series, HP-49 series
, TI-89, and TI-Nspire, or the TI-83 Plus/TI-84 Plus with the PrettyPt add-on, can perform pretty-printing. Additionally, a number of newer scientific calculators are equipped with dot matrix screens capable of pretty-printing such as the Casio FX-ES series (Natural Display), Sharp EL-W series (WriteView), HP SmartCalc 300s, and TI-30XB.
Many text formatting programs can also typeset mathematics: TeX
was developed specifically for high-quality mathematical typesetting.
source code
in a particular manner. Proper code formatting makes it easier to read and understand. Moreover, often different programmers have different preferred styles of formatting, such as the use of code indentation
and whitespace or positioning of braces
. A code formatter converts source code from one format style to another. This is relatively straightforward because of the unambiguous syntax of programming languages. Code beautification involves parsing the source code into component structures, such as assignment statements, if blocks, loops, etc. (see also control flow
), and formatting them in a manner specified by the user in a configuration file.
There exist both standalone code beautifiers and ones built into integrated development environment
s and text editor
s. For example, Emacs
' various language modes can correctly indent
blocks of code attractively.
's "GRIND" program, which used combinatorial search
with pruning to format LISP
programs. The term "grind" was used in some Lisp circles as a synonym for pretty-printing.
rules format them. Without any formatting at all, it looks like this:
The GNU indent program
produces the following output when asked to indent according to the GNU rules:
It produces this output when formatting according to BSD rules:
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...
, markup
Markup language
A markup language is a modern system for annotating a text in a way that is syntactically distinguishable from that text. The idea and terminology evolved from the "marking up" of manuscripts, i.e. the revision instructions by editors, traditionally written with a blue pencil on authors' manuscripts...
, and other similar kinds of content. These formatting conventions usually consist of changes in positioning, spacing, color, contrast, size and similar modifications intended to make the content easier for people to view, read, and understand. Prettyprinters for programming language source code are sometimes called code beautifiers or syntax highlighters
Syntax highlighting
Syntax highlighting is a feature of some text editors that display text—especially source code—in different colors and fonts according to the category of terms. This feature eases writing in a structured language such as a programming language or a markup language as both structures and...
.
Pretty-printing mathematics
Pretty-printing usually refers to displaying mathematical expressions in a way that is similar to the way they are typesetFormula editor
A formula editor is a name for a computer program that is used to typeset mathematical works or formulae.Formula editors typically serve two purposes:...
professionally. For example, in computer algebra system
Computer algebra system
A computer algebra system is a software program that facilitates symbolic mathematics. The core functionality of a CAS is manipulation of mathematical expressions in symbolic form.-Symbolic manipulations:...
s such as Maxima or Mathematica
Mathematica
Mathematica is a computational software program used in scientific, engineering, and mathematical fields and other areas of technical computing...
the system may write output like "x ^ 2 + 3 * x" as "". Some graphing calculator
Graphing calculator
A graphing calculator typically refers to a class of handheld calculators that are capable of plotting graphs, solving simultaneous equations, and performing numerous other tasks with variables...
s, such as the Casio 9860 series, HP-49 series
HP-49 series
The HP 49G series are Hewlett-Packard manufactured graphing calculators. They are the successors of the popular HP-48 series.There are four calculators in the 49 series of HP graphing calculators...
, TI-89, and TI-Nspire, or the TI-83 Plus/TI-84 Plus with the PrettyPt add-on, can perform pretty-printing. Additionally, a number of newer scientific calculators are equipped with dot matrix screens capable of pretty-printing such as the Casio FX-ES series (Natural Display), Sharp EL-W series (WriteView), HP SmartCalc 300s, and TI-30XB.
Many text formatting programs can also typeset mathematics: 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 ....
was developed specifically for high-quality mathematical typesetting.
Code formatting and beautification
Programmers often use tools to format programming languageProgramming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...
in a particular manner. Proper code formatting makes it easier to read and understand. Moreover, often different programmers have different preferred styles of formatting, such as the use of code indentation
Indentation
An indentation may refer to:* A notch, or deep recesses; for instance in a coastline, or a carving in rock* The placement of text farther to the right to separate it from surrounding text....
and whitespace or positioning of braces
Bracket
Brackets are tall punctuation marks used in matched pairs within text, to set apart or interject other text. In the United States, "bracket" usually refers specifically to the "square" or "box" type.-List of types:...
. A code formatter converts source code from one format style to another. This is relatively straightforward because of the unambiguous syntax of programming languages. Code beautification involves parsing the source code into component structures, such as assignment statements, if blocks, loops, etc. (see also control flow
Control flow
In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....
), and formatting them in a manner specified by the user in a configuration file.
There exist both standalone code beautifiers and ones built into integrated development environment
Integrated development environment
An integrated development environment is a software application that provides comprehensive facilities to computer programmers for software development...
s and text editor
Text editor
A text editor is a type of program used for editing plain text files.Text editors are often provided with operating systems or software development packages, and can be used to change configuration files and programming language source code....
s. For example, 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...
' various language modes can correctly indent
Indentation
An indentation may refer to:* A notch, or deep recesses; for instance in a coastline, or a carving in rock* The placement of text farther to the right to separate it from surrounding text....
blocks of code attractively.
Early Example
An early example of pretty-printing was Bill GosperBill Gosper
Ralph William Gosper, Jr. , known as Bill Gosper, is an American mathematician and programmer from Pennsauken Township, New Jersey...
's "GRIND" program, which used combinatorial search
Combinatorial optimization
In applied mathematics and theoretical computer science, combinatorial optimization is a topic that consists of finding an optimal object from a finite set of objects. In many such problems, exhaustive search is not feasible...
with pruning to format LISP
Lisp programming language
Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older...
programs. The term "grind" was used in some Lisp circles as a synonym for pretty-printing.
Project style rules
Many open source projects have established rules for code layout. The most typical are the GNU style and the BSD style. The biggest difference between the two is the location of the braces: in the GNU style, opening and closing braces are on lines by themselves, with the same indent. BSD style places an opening brace at the end of the preceding line, and the closing braces can be followed by else. The size of indent and location of white space also differs.Example of formatting and beautifying code
The following example shows some typical C structures and how various indentation styleIndent style
In computer programming, an indent style is a convention governing the indentation of blocks of code to convey the program's structure. This article largely addresses the C programming language and its descendants, but can be applied to most other programming languages...
rules format them. Without any formatting at all, it looks like this:
The GNU indent program
Indent (Unix)
indent is a Unix utility that reformats C and C++ code in a user-defined indent style and coding style. Support for C++ code is considered experimental.-Examples of usage:The command line...
produces the following output when asked to indent according to the GNU rules:
It produces this output when formatting according to BSD rules:
See also
- Elastic tabstopElastic tabstopIn text editor applications in computing, elastic tabstops are an alternative way to handle tabstops, with a primary focus on editing source code in computer programming. The idea was first proposed by Nick Gravgaard as a solution for programmers who argue about what kind of indentation is best;...
, an indentationIndent styleIn computer programming, an indent style is a convention governing the indentation of blocks of code to convey the program's structure. This article largely addresses the C programming language and its descendants, but can be applied to most other programming languages...
technique that leads to automatic pretty-printing in advanced source code editorSource code editorA source code editor is a text editor program designed specifically for editing source code of computer programs by programmers. It may be a standalone application or it may be built into an integrated development environment ....
s - enscriptEnscriptGNU enscript is a computer program that converts text files to PostScript, RTF, or HTML formats. If no input files are given, enscript processes standard input...
, a general text printing tool with prettyprinting functions - indentIndent (Unix)indent is a Unix utility that reformats C and C++ code in a user-defined indent style and coding style. Support for C++ code is considered experimental.-Examples of usage:The command line...
- XML Pretty Printer
External links
- Algorithm 268: ALGOL 60 reference language editor W. M. McKeeman: Commun. ACM 8(11): 667-668 (1965)
- NEATER2: a PL/I source statement reformatter Kenneth Conrow, Ronald G. Smith: Commun. ACM 13(11): 669-675 (1970)
- SOAP - A Program which Documents and Edits ALGOL 60 Programs. R. S. Scowen, D. Allin, A. L. Hillman, M. Shimell: Comput. J. 14(2): 133-135 (1971)
- PRETTYP.PAS Early pascal prettyprinter. Ledgard et al.: Pascal With Style (1979)
- style(9) FreeBSD style guidelines
- Formatting your source code GNU style guidelines