Prettyprint
   HOME

TheInfoList



OR:

Pretty-printing (or prettyprinting) is the application of any of various stylistic formatting conventions to
text file A text file (sometimes spelled textfile; an old alternative name is flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system. In operat ...
s, such as
source code In computing, source code, or simply code, is any collection of code, with or without comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the ...
, markup, and similar kinds of content. These formatting conventions may entail adhering to an
indentation style In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure. This article largely addresses the free-form languages, such as C and its descendants, but can be (and often ...
, using different color and typeface to highlight syntactic elements of source code, or adjusting size, to make the content easier for people to read, and understand. Pretty-printers for source code are sometimes called code formatters or beautifiers.


Pretty-printing mathematics

Pretty-printing usually refers to displaying
mathematical expression In mathematics, an expression or mathematical expression is a finite combination of symbols that is well-formed according to rules that depend on the context. Mathematical symbols can designate numbers ( constants), variables, operations, f ...
s similar to the way they would be typeset professionally. For example, in
computer algebra system A computer algebra system (CAS) or symbolic algebra system (SAS) is any mathematical software with the ability to manipulate mathematical expressions in a way similar to the traditional manual computations of mathematicians and scientists. The ...
s such as Maxima or
Mathematica Wolfram Mathematica is a software system with built-in libraries for several areas of technical computing that allow machine learning, statistics, symbolic computation, data manipulation, network analysis, time series analysis, NLP, optimiza ...
the system may write output like "x ^ 2 + 3 * x" as Some
graphing calculator A graphing calculator (also graphics calculator or graphic display calculator) is a handheld computer that is capable of plotting graphs, solving simultaneous equations, and performing other tasks with variables. Most popular graphing calcu ...
s, such as the
Casio 9860 series The Casio fx-9860G is a series of graphing calculators manufactured by Casio, successor of the fx-9750G PLUS/CFX-9850 PLUS/CFX-9950 PLUS/CFX-9970 family of calculators. All of them are capable of being programmed by means of sophisticated languages ...
,
HP-49 series The HP 49/50 series are Hewlett-Packard (HP) manufactured graphing calculators. They are the successors of the popular HP 48 series. There are five calculators in the 49/50 series of HP graphing calculators. These calculators have both ...
, TI-84 Plus,
TI-89 The TI-89 and the TI-89 Titanium are graphing calculators developed by Texas Instruments (TI). They are differentiated from most other TI graphing calculators by their computer algebra system, which allows symbolic manipulation of algeb ...
, and
TI-Nspire The TI-Nspire is a graphing calculator made by Texas Instruments, which was released in July 2007, notable for featuring a non-QWERTY keyboard and a different key-by-key layout than its predecessors. Development The original TI-Nspir ...
, the TI-83 Plus with the PrettyPt add-on, or the TI-84 Plus with the same add-on or the "MathPrint"-enabled OSes, 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, TI-30XB, and Numworks. Many text formatting programs can also typeset mathematics: TeX was developed specifically for high-quality mathematical
typesetting Typesetting is the composition of text by means of arranging physical ''type'' (or ''sort'') in mechanical systems or '' glyphs'' in digital systems representing '' characters'' (letters and other symbols).Dictionary.com Unabridged. Random ...
.


Pretty-printing markup and tag-based code

Pretty-printing in markup language instances is most typically associated with indentation of tags and string content to visually determine hierarchy and nesting. Although the syntactical structures of tag-based languages do not significantly vary, the indentation may vary significantly due to how a markup language is interpreted or due to the data it describes. In
MathML Mathematical Markup Language (MathML) is a mathematical markup language, an application of XML for describing mathematical notations and capturing both its structure and content. It aims at integrating mathematical formulae into World Wide W ...
,
whitespace character In computer programming, whitespace is any character or series of characters that represent horizontal or vertical space in typography. When rendered, a whitespace character does not correspond to a visible mark, but typically does occupy an area ...
s do not reflect data, meaning, or syntax above what is required by
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. T ...
syntax. In
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaS ...
, whitespace characters between tags are considered text and are parsed as text nodes into the parsed result. While indentation may be generously applied to a MathML document, sufficient additional care must be taken in pretty-printing an HTML document to ensure additional text nodes are not created or destroyed in general proximity to the content or content-reflective tag elements. This difference in complexity is non-trivial from the perspective of an automated pretty-print operation where no special rules or edge cases are necessary, as in the more simple MathML example. The HTML example may require a series of progressive interrelated algorithms to account for various patterns of tag elements and content that conforms to a uniform style and is consistent in application across various instances, as evidenced by the markup.ts application component used to beautify HTML, XML, and related technologies for the Pretty Diff tool.


Programming code formatting

Programmers often use tools to format
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
source code In computing, source code, or simply code, is any collection of code, with or without comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the ...
in a particular manner. Proper code formatting makes it easier to read and understand. Different programmers often prefer different 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,
loop Loop or LOOP may refer to: Brands and enterprises * Loop (mobile), a Bulgarian virtual network operator and co-founder of Loop Live * Loop, clothing, a company founded by Carlos Vasquez in the 1990s and worn by Digable Planets * Loop Mobile, an ...
s, etc. (see also
control flow In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an '' ...
), and formatting them in a manner specified by the user in a configuration file. Code beautifiers exist as standalone applications and built into
text editor A text editor is a type of computer program that edits plain text. Such programs are sometimes known as "notepad" software (e.g. Windows Notepad). Text editors are provided with operating systems and software development packages, and can be ...
s and
integrated development environment An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools ...
s. For example,
Emacs Emacs , originally named EMACS (an acronym for "Editor MACroS"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, ...
' various language modes can correctly indent blocks of code attractively.


HTML


Lisp pretty-printer

An early example of pretty-printing was
Bill Gosper Ralph William Gosper Jr. (born April 26, 1943), known as Bill Gosper, is an American mathematician and programmer. Along with Richard Greenblatt, he may be considered to have founded the hacker community, and he holds a place of pride in the ...
's "GRINDEF" (''i.e.'' 'grind function') program (''c.'' 1967), which used
combinatorial search {{no footnotes, date=January 2013 In computer science and artificial intelligence, combinatorial search studies search algorithms for solving instances of problems that are believed to be hard in general, by efficiently exploring the usually large ...
with pruning to format
LISP A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispin ...
programs. Early versions operated on the executable (list structure) form of the Lisp program and were oblivious to the special meanings of various functions. Later versions had special read conventions for incorporating non-executable comments and also for preserving read macros in unexpanded form. They also allowed special indentation conventions for special functions such as if. 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 GNU () is an extensive collection of free software (383 packages as of January 2022), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operat ...
formatting and the BSD style.BSD style
/ref> 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 whitespace also differs.


Example of formatting and beautifying code

The following example shows some typical C structures and how various
indentation style In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure. This article largely addresses the free-form languages, such as C and its descendants, but can be (and often ...
rules format them. Without any formatting at all, it looks like this: int foo(int k) The GNU indent program produces the following output when asked to indent according to the GNU rules: int foo (int k) It produces this output when formatting according to BSD rules: int foo(int k)


See also

Related concepts * Elastic tabstop, a feature of many source code editors that detects and maintains aligned indents * Minification, making source code compact, even if it becomes harder for humans to understand *
Obfuscation Obfuscation is the obscuring of the intended meaning of communication by making the message difficult to understand, usually with confusing and ambiguous language. The obfuscation might be either unintentional or intentional (although intent ...
, deliberately making source code very difficult for humans to understand, even if it becomes somewhat convoluted Utilities * enscript, a text-to-PostScript converter, with pretty-printing features


References

{{reflist


External links


Algorithm 268: ALGOL 60 reference language editor
'' William M. McKeeman'': Commun. ACM 8(11): 667-668 (1965)
lgrind
Comprehensive TEX Archive Network
NEATER2: a PL/I source statement reformatter
''Kenneth Conrow, Ronald G. Smith'': Commun. ACM 13(11): 669-675 (1970)
SOAP - Simplify Obscure Algol Programs
''R. S. Scowen, D. Allin, A. L. Hillman, M. Shimell'': National Physical Laboratory Central Computer Unit repor
CCU6
(April, 1969) Includes formatted listing of SOAP source code. *
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) *
Original SOAP Source Code from the KDF9 (unformatted)

SOAP User's Guide.
(for Edinburgh IMP) ''Peter Salkeld Robertson'' (1976) *
SOAP Source Code
in/for IMP9
Soap80: A Program for Formatting IMP80 Source Programs.
''J.M. Murison, Edinburgh Regional Computer Center'' (1980) *

in/for IMP80 ''E. N. Gregory, University of Kent at Canterbury; Peter D. Stephens, Edinburgh Regional Computer Center''
PRETTYP.PAS
Early pascal prettyprinter. ''Ledgard et al.''
Pascal With Style
(1979)
style(9)
FreeBSD style guidelines

The
nix Nix or NIX may refer to: Places * Nix, Alabama, an unincorporated community, United States * Nix, Texas, a ghost town in southwestern Lampasas County, Texas, United States * Nix (moon), a moon of Pluto People * Nix (surname), listing people wit ...
Heirloom Project
Formatting your source code
GNU style guidelines Articles with example C code Source code Text editor features